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'm using the following code to retrieve posts:

$featuredPosts = new WP_Query();
while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<div class="meta">
By <?php the_author() ?>
<div class="storycontent">
<?php the_excerpt(); ?>
<?php endwhile; ?>

Do I need to use wp_reset_query() If I have to use it where should I place it?

share|improve this question
If you're relying on the main query object somewhere else in the page, then Yes! you should call it, to ensure the main query object contains the data is did before you iterated over your custom query. When you call the_post() method(ie. $my_custom_query->the_post()) you refill the post variables the main query looks at, the reset refills these vars with the previous data when you call it. It is good practice to use resets after custom queries. – t31os Jan 29 '11 at 12:32
up vote 9 down vote accepted

Hi @janoChen:

Simple answer: no.

What follows is what the PHP code for the function wp_reset_query() from /wp-includes/query.php in WordPRess v3.0.4 as well as the functions subsequently called. You can see that it's primarily about in modifying global variables.

When you use new WP_Query($args) you will be assigning the return value from values to a local variable so, unless you are doing something so complex that you'd already know the answer to this question for then no, you don't need to call wp_reset_query():

function wp_reset_query() {
  $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];

function wp_reset_postdata() {
  global $wp_query;
  if ( !empty($wp_query->post) ) {
    $GLOBALS['post'] = $wp_query->post;

function setup_postdata($post) {
  global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;

  $id = (int) $post->ID;

  $authordata = get_userdata($post->post_author);

  $day = mysql2date('d.m.y', $post->post_date, false);
  $currentmonth = mysql2date('m', $post->post_date, false);
  $numpages = 1;
  $page = get_query_var('page');
  if ( !$page )
    $page = 1;
  if ( is_single() || is_page() || is_feed() )
    $more = 1;
  $content = $post->post_content;
  if ( strpos( $content, '<!--nextpage-->' ) ) {
    if ( $page > 1 )
      $more = 1;
    $multipage = 1;
    $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
    $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
    $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
    $pages = explode('<!--nextpage-->', $content);
    $numpages = count($pages);
  } else {
    $pages = array( $post->post_content );
    $multipage = 0;

  do_action_ref_array('the_post', array(&$post));

  return true;


share|improve this answer
Thanks again, I think you will never be beaten by Rarst. – janoChen Jan 29 '11 at 7:17
@janoChen - heh. He's definitely been pushing me lately, that's for sure! I guess as they say, competition improves the breed (but sure keeps me from getting anything else productive done! '-) – MikeSchinkel Jan 29 '11 at 7:20
Just for others reading this, since this is still the accepted answer (@Rarst's answer should be accepted). Since the OP uses the_post() in his code, best practices dictate that he must use wp_reset_postdata(). wp_reset_query() calls wp_reset_postdata(), so that will work, though the other thing wp_reset_query() does - resetting the $wp_query global variable - is not necessary, but not harmful in this case. So the answer is actually YES – Tom Auger Jul 22 '13 at 19:22

It is not necessary for WP_Query by itself, but it is necessary (or at least good thing to do) if you use any related functions/methods ( such as the_post() or setup_postdata() ) to fill global variables with your data.

Basically creating new WP_Query object is merely data retrieval, but using it to run active loop and making data accessible to template tags does modify environment and it is good to reset everything afterwards.

Overall - it isn't meaningful performance penalty to call it, so it is easier to always call it than deciding if you should or forgetting about it and having something mysteriously broken.


wp_reset_postdata() function seems to be more fitting choice. wp_reset_query() resets global $wp_query (which custom WP_Query object doesn't affect) and $post (which it might as above) variables. wp_reset_postdata() only restores $post, which should be enough.

share|improve this answer
Thanks for updating the answer, that was helpful. – Jonathan Wold Apr 27 '11 at 3:04

No. If you instantiate your own WP_Query object, its yours to do with what you will. However, if you tamper with the global $wp_query variable, well, then your in the global namespace affecting anyone's script who is concurrently using that variable. And if you do something to change the data in it, you must also reset it after your done using it.

share|improve this answer

If you are using custom query like this

$cat = new WP_query(); 
while ($cat->have_posts()) : $cat->the_post(); 
  $data = get_post_meta( $post->ID, 'key', true );
$img_arrays []= $data['productimage']; 
$lnk_arrays[] =get_permalink($post_ID); 

Then you won't run into problems. Otherwise, if on the same page there is another loop you are bound to get some unexpected results. I didn't used wp_reset_query() in the above code (which was placed in my header.php file. Then when i got into single.php mostly i get the detail pages of other categories which was frustrating. Later, i realized that i forgot to reset the query at the top. Soon, it started working like a charm.

share|improve this answer

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.