WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#9425 closed defect (bug) (wontfix)

The various $is_* variables in $WP_Query should only be set on the very first query call.

Reported by: mtg169 Owned by:
Milestone: 2.8 Priority: high
Severity: major Version: 2.7.1
Component: General Keywords: needs-patch dev-feedback
Focuses: Cc:

Description

Thought I had a problem with a plugin, but it appears to be a bug with query.php. On the home page, when I do the following query, is_home returns true:
query_posts("cat=-1,-6,-11,-21,-23,-24,-27,-35,-38,-40&posts_per_page=1");

Ideally I would like to specify included categories only so I do not have to modify the code every time a new category is added. If I use the following query, is_home always returns false even though I am on the home page:
query_posts("cat=3,4,5,7&posts_per_page=1");

The only way I can get is_home to return true is if I exclude at least one category (could even be -0).

For the mean time I have just specifically defined is_home=true on my home page, rather than having query.php set it incorrectly.

Change History (11)

comment:1 follow-up: Viper007Bond5 years ago

Does using $query_string fix this?

comment:2 hakre5 years ago

This is not a Bug in the WordPress Core, it is a 'Bug' in an individual Theme, if you want to call it Bug at all.

To circumvent the (knwon and wanted) Side-Effects between is_home() and query_posts(), first call is_home() and store the return value in a variable.

comment:3 hakre5 years ago

  • Resolution set to wontfix
  • Status changed from new to closed

comment:4 Denis-de-Bernardy5 years ago

  • Milestone changed from Unassigned to 2.8
  • Priority changed from normal to high
  • Resolution wontfix deleted
  • Severity changed from normal to major
  • Status changed from closed to reopened
  • Summary changed from is_home returns false when specifying included categories only to The various $is_* variables in $WP_Query should only be set on the very first query call.

I disagree. I ran into this one myself. For what it's worth, my conclusion at the time was that I should avoid the WP API and its built-in widgets at all costs. Because they were completely broken.

It has improved since, but this is one of those areas where it still needs improvements.

The fact of the matter is that the poor API and the poor documentation has led theme developers to actually use functions such as the above. They inconveniently reset the "true" query's internal variables, and you can then get is_category() to answer true when is_home() should and what not.

The various $is_* variables in $WP_Query should only be set on the very first query call. Not on subsequent ones.

comment:5 Denis-de-Bernardy5 years ago

  • Keywords needs-patch dev-feedback added; is_home query.php cat= removed

comment:6 follow-up: hakre5 years ago

Denis, I understand your disappointment and I do understand the problems you run into very well. But those are WordPress related. wp_query is one of the most central functions in wordpress data processing.

Because the querying and displaying of data in WordPress is absolutely connected (in opposite to seperated, many WP devs call that 'lighweight design') you can not say A without saying B.

That means: wp_query manipulates basic global variables that other parts of wordpress is affected of.

that is the principle of a global variable.

you can not go around it unless central parts of wordpress will be modularized apart from each other.

for example having a query class (multiple instances), and having _one_ frontend query (the main query instance) which then is check against in is_home() (just an example).

anyway, I do not want to argument against your concerns, because I strongly feel with you. The question is where to draw the line.

Maybe this bug should be tagged "architectural-changes"?

comment:7 in reply to: ↑ 6 Denis-de-Bernardy5 years ago

Oh, don't get me wrong, I was merely pointing out that he was highlighting a genuine issue in the WP API. The immediate closing as wontfix had me chew on the number of times where I went "This WP revision will introduce scores of bugs, but to hell with it".

Maybe this bug should be tagged "architectural-changes"?

Probably. Imo, merely generating a new WP_Query object (while keeping the $wp_the_query object around for the WP internals) on every call to query_posts() would fix this. But what do I know... I've only been using WP for years. ;-)

D.

comment:8 in reply to: ↑ 1 mtg1695 years ago

Replying to Viper007Bond:

Does using $query_string fix this?

No, I get the same results when using $query_string. So the $is_* variables are reset every time query_posts is called? It makes sense now. I was checking $is_home before running query_posts and query_posts was only running if $is_home was true. It was confusing me because query_posts would run ($is_home was true) but then $is_home was reset to false after running query_posts unless I had at least one category excluded.

comment:9 jbsil5 years ago

Sounds like this is still 'wontfix' for 2.8. Should it be closed or milestone changed?

comment:10 ryan5 years ago

wp_reset_query() exists to restore the original query after calling finished with a custom query_posts() loop. I think that will have to be good enough for now.

comment:11 Denis-de-Bernardy5 years ago

  • Resolution set to wontfix
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.