WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I include this logic in my template all the time

if ( have_posts() ):
 //show content
 //show content not found

But recently I began to doubt its necessity, WordPress will turn to 404.php when no post found, it seems no need to add this logic in normal templates, else will never be triggered, is it?

share|improve this question
up vote 6 down vote accepted

When I look at WP::handle_404(), I think that the 404.php template will not be loaded, even if there are no posts, if:

  • We are on the homepage
  • It is a search
  • It is a taxonomy term that exists, but has no posts attached to it (and it's not paged)
  • It is an author that exists, which has written no posts (yet) (and it's not paged)

So in these template files, have_posts() can be useful.

share|improve this answer
Makes sense. I've run into a case myself when loading the standard 404 template would not be desirable. – scribu May 6 '11 at 1:27

No, in most cases it's not needed.

if ( have_posts() ) would only be useful on the index.php file, if you don't have a 404.php template.

And even in that case, you could use is_404() instead.

share|improve this answer
When I look at the code, I see some cases where the 404.php file will not be loaded. I provided details in my answer. – Jan Fabry May 5 '11 at 7:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.