Skip to content

Instantly share code, notes, and snippets.

@mehedicsit
Last active August 29, 2015 14:13
Show Gist options
  • Save mehedicsit/43b89fd6e2f1bee234f2 to your computer and use it in GitHub Desktop.
Save mehedicsit/43b89fd6e2f1bee234f2 to your computer and use it in GitHub Desktop.
wordpress query post loop
http://wordpress.stackexchange.com/questions/117219/why-should-i-put-ifhave-posts-is-whilehave-posts-not-enough
/*this is the main link of query post expalnation*/
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
/*this is the explanation of why we will use post loop*/
The WordPress template loader will include the appropriate contextual template file in many circumstances, even if the query for that context returns no posts. For example:
The Main Blog Posts Index
Category Archive Index (Category exists, but has no posts)
Tag Archive Index (Tag exists, but has no posts)
Author Archive Index (Author exists, but has no posts)
Search Results Index
Thus, in these cases, the appropriate template file will be loaded, but no posts will be output, because the query returns no posts.
Proof-of-concept examples:
Category exists, but has no posts (category template included)
Category does not exist (404 template included)
So, in these contexts, it is useful for the template file to include the if ( have_posts() ) conditional.
In other contexts, the template file will never be loaded if the query returns no posts. For example:
Single blog post
Static page
In these contexts, if ( have_posts() ) is probably unnecessary.
Edit
I understand the query is invoked by the_post(), right? And if while(have_posts()) exist, the query is never occurs if there is no post.
To understand what's going on, you have to look at the order of WordPress actions. Starting with wp_loaded (and omitting some for clarity):
wp_loaded
parse_request
send_headers
parse_query
pre_get_posts
wp
template_redirect
get_header
wp_head
the_post
wp_footer
So, what's happening, and in what order?
The query is invoked:
parse_query
pre_get_posts
wp
The template is selected:
template_redirect
The template is loaded/output. The following actions are fired by the template:
get_header
wp_head
the_post
dynamic_sidebar
get_footer
wp_footer
So, the_post, fired by the_post(), happens long after the query is parsed, posts are fetched, and the template is loaded.
I'm very grateful that you give a lot of information that I didn't know, but this is not what I asked.
Oh, but I believe that it is exactly what you asked.
The real question is: what is a valid query return? For contexts such as the category archive index, the query is valid, and the category template is loaded, if the queried category ID exists, even if there are no posts assigned to that category.
Why? Because the query being parsed is (IIRC) &cat={ID} - which is a valid query even if there are no posts assigned to that category, and thus doesn't result in a 404 upon parsing.
In that case, you get a valid query, and a template file loaded, but no posts. Thus, if ( have_posts() ), is, in fact relevant. Again, here is an example: category exists, but has no posts assigned. Category template file is loaded, with if ( have_posts() ) returning false.
This won't hold true for queries that include a post variable (&p={ID}) such as single blog posts and static pages, because the post won't actually exist, and when parsed, the query will not return a valid object.
Edit 2
If I rightly understand if there is no if(have_posts()) in a category template and the category have no post, then it return 404.php, even though it should be return category-sample.php without post. Is that right?
No. Remember: the template is selected at template_redirect. So if the query if valid, then the appropriate template file is loaded. If the query is not valid, then the 404 template is loaded.
So, once a template is loaded - e.g. the category template - once the loop is output, the template does not change.
Look again at the order of actions:
parse_query
pre_get_posts
wp
template_redirect - template is chosen and loaded here. This is the template point of no return. The template cannot change after this point.
...
the_post - postdata is setup here, as part of the loop call. This is called inside the template, and the template does not change based on available data i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment