Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

global $more;
$more = 0;
query_posts('cat=3');
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>
     <?php
     endwhile;
endif;
wp_reset_query();?>
share|improve this question
    
possible duplicate of When to use WP_query(), query_posts() and pre_get_posts –  Chip Bennett Mar 9 '13 at 17:22
add comment

1 Answer

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:

http://www.slideshare.net/andrewnacin/you-dont-know-query-wordcamp-netherlands-2012

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

share|improve this answer
    
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
add comment

Your Answer

 
discard

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.