Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#7472 closed defect (bug) (invalid)

query_posts() kills is_404 flag

Reported by: donncha Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.6
Component: General Keywords:
Focuses: Cc:


Repeated usage of query_posts() destroys the 404 status flag for a page.

This probably hasn't been noticed before because the 404.php theme file will have been selected by the time query_posts() is used (say in a theme).

How to reproduce:

Add this code to your sidebar: (or any other query_posts() call that generates posts)

In the footer of your theme's index.php (or 404.php if it's there) add this code:
<?php if( is_404() ) { echo "<p>" . $_SERVER[ 'REQUEST_URI' ] . " is a 404</p>"; } ?>

Add and remove the query_posts() call. The "is a 404" message will disappear and reappear. query_posts() creates a new global WP_Query and all status flags get overwritten.

I discovered this bug while trying to figure out why wp-super-cache was caching 404 pages. Eventually I used the template_redirect action to remember the 404 status for use later on. People have complained about wp-super-cache caching 404 pages for quite some time so this is probably a long standing bug.

Change History (4)

comment:1 @ryan7 years ago

  • Milestone 2.6.1 deleted
  • Resolution set to invalid
  • Status changed from new to closed

Isolated to an MU change.


comment:2 @ryan7 years ago

Oops, that's slightly different, but I'm not sure what there is to do here anyway. query_posts() uses the global query. If you don't want to stomp the global query, don't use query_posts().

comment:3 @donncha7 years ago

Apparently plenty of themes use it in the sidebar and the Codex doesn't mention the 404 side effect. Perhaps the documentation should be updated?

comment:4 @santosj7 years ago

I'm pretty sure it does say something about screwing up the main Loop. It also says that the alternative is to use get_posts() or create your own WP_Query object. You just can't go around overriding globals and expecting stuff to work.

It isn't WordPress's fault.

Note: See TracTickets for help on using tickets.