I am not able to understand why we need to use wp_reset_query(); could anybody explain me?

global $more;
$more = 0;
if(have_posts()) :
     while(have_posts()) :the_post();
         <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
         <div><?php the_content('Read More') ?></div>

When you call query_post it replaces the main query object with one of its own, but that isn't desirable. If you want to return to the main query, you have to call wp_reset_query to do so whichr estores the original query.

I'd advise strongly against the use of query_posts and instead suggest you use WP_Query. Query_posts is wasteful, slow, encourages bad practices, does not nest, and can get incredibly confusing.

I recommend you read this:


Its a presentation by a core developer, once you've read that you should know everything you will ever need to know, it contains all the important things that normally takes people years to learn, about how to do querys, what kinds and when to use them, and how to modify them

  • But, should wp_reset_query be used with WP_Query? Or is it exclusive for query_posts? From your A, I understand that: 1) No and 2) Yes; but IMO it's not very clear...
    – brasofilo
    Mar 9 '13 at 12:00
  • 1
    Look at the "you dont know query" slides I linked to, they're veyr to the point and tell you everything you could ever need to know, about how to do things, and why
    – Tom J Nowell
    Mar 9 '13 at 16:53
  • query_posts replaces the main query object, WP_Query does not. wp_reset_query cleans up after query_posts by restoring the main query object. I would strongly recommend against use of the query_posts function. I can not stress how important it is that you remove that function and all traces of it from your code. It is incredibly bad practice.
    – Tom J Nowell
    Mar 9 '13 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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