Apology: Not Useless Code

at the top of the main loop, have_posts() and is_404() are mutually exclusive.

Useless Code in WordPress Themes

That is true sometimes, but not always. More specifically, it can be false when a non-permalink URL seeks a non-existent post. For example, /?p=-1.

Issue a 404 if a permalink request doesn’t match any posts. Don’t issue a 404 if one was already issued, if the request was a search, or if the request was a regular query string request rather than a permalink request.


Now it makes sense that the single.php template file would check have_posts(). Let the healing begin.

(Are there any documented examples of this bug occurring outside of single.php?)

Previous Post


  1. I had sort of forgotten about this. I would consider it extra strength usage. Which is good!

    So if /?page_id=9999 is a regular query for an invalid page, shouldn’t a 404 be issued, eg, sndbx.org/?page_id=9999? Perhaps we should tag this for Sandbox. ;-)

  2. Scott: Confirmed: the page.php template also needs if ( have_posts() ). Thanks!

    I was working on a patch to add Sandbox to core when this problem resurfaced. That’s why this post. :)

    I do not know why WordPress does not issue 404s for these queries and use the 404.php template. It is intentional but the reason is undocumented. Anybody know?

  3. Glad you finally come clean ;-) Though I thought you would have fix core to make your lie a truth. (If I recall that was partially done was an example of trying to view a draft when logged out using the Sandbox was uncovered last year)

  4. Hacking core to prove yourself right is a completely valid course of action ;)

  1. Weekly Digest: Writing About Writing, Working on Books, Lots of Speaking Gigs, and Blog Struggles is a Success « Lorelle on WordPress

Get every new post delivered to your Inbox.

Join 1,951 other followers

%d bloggers like this: