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

The function below is called from my index.php just below the main content block. Its purpose is to write out a link list of the latest posts. However, I need to reset the post object once this function has completed (or the sidebar query that follows thinks that the current post is the last post that gets executed by this function.)

How should I reset the post back to the current post of the page?

I've tried adding wp_reset_query() at the end of the function, but its not producing the results I want (produces multiple posts in the content area). Any help much appreciated.

function myFunction(){
    $myquery = new WP_Query();
    $myquery->query(array('cat' => "-$catHidden",'post__not_in' => get_option('sticky_posts')));
    $myrecentpostscount = $myquery->found_posts;
    if ($myrecentpostscount > 0){ ?>
            <h4>Menu Title</h4>
            global $post;
            $myrecentposts = get_posts(array('post__not_in' => get_option('sticky_posts'), 'cat' => "-$catHidden",'numberposts' => get_option('latest_count')));
            foreach($myrecentposts as  $idxrecent=>$post) { ?>
                <li class="page_item">
                    <a href="<?php the_permalink(); ?>"><?php if(has_post_thumbnail() && get_option('show_thumbs')) the_post_thumbnail('thumbnail', array('class' => 'alignleft', 'style' => 'margin:0 10px 0 0;')); ?><?php the_title(); ?></a>
                        if(has_post_thumbnail() && get_option('show_thumbs')) echo '<div style="clear:both">&nbsp;</div>';?>
            <?php } 
    echo "</ul></div>";}}
share|improve this question
up vote 5 down vote accepted

wp_reset_query() will reset the query to the original query WordPress did on this page. So if you somewhere called query_posts(), it will not reset back to that query, but to the "main" query.

You probably want to use wp_reset_postdata(), which resets the $post variable to the current post in $wp_query.

The best thing is to not overwrite the global $post variable in your function. All WordPress functions have variants that accept a post object, so there should be no need to use the global variable there.

share|improve this answer
That was it Jan. I added wp_reset_postdata() to the end of the function and now all is well. However, I was not able to get the function to work without setting global $post. Even passing $post into the function does not work as expected. – Scott B May 26 '11 at 15:26

I think the problem here may have to do with the uses of including the global $post statement, as well as using get_posts.

you're essentially calling for the posts twice, one with the new WP_Query (just to check the count of the posts returned), and the second time using get_posts.

the first time you check it, it has returned the posts under that main variable $myquery->posts;

you also don't want to use found_posts, as that doesn't appear to return the right number. use $myquery->post_count instead.

once you get rid of the global $post, and the get_posts, change over the found_posts variable - you won't have any more polution of data. i've used this:

$posts=new WP_Query(//array details);
if($posts->have_posts()) : 
    while($posts->have_posts()) : $posts->the_post();

        // do stuff with the post, if you need things like the_permalink, use get_permalink($thispost->ID) instead.

and it didn't ruin my data at all. though it's a tiny bit more of a hassle since you can't use the regular the_title, the_content, etc.. but you can use the get ones based on the id's anyway - and if you need those specific filters for the regulars, you can always make use of them on the pulled content by applying them as well.

share|improve this answer
Thanks Mike. I'll check this out. Makes sense to me. – Scott B May 26 '11 at 15:29

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.