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?)

5 thoughts on “Apology: Not Useless Code”

  1. 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?

  2. 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)

Comments are closed.