Don’t Forget wp-reset-query When Using WordPress Hacks
March 7th, 2010 by
Before I launched my blog (about 5 weeks ago), one of the things I looked for, was a clean and easy way to display related posts without installing a plugin.
After a quick search I found this post with the following code:
<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
}
?>
It was all nice and simple until I received my first 2 comments and noticed something strange:
Wordpress assigned the comments to a different post than the one they were meant for.
If it was a single comment I might have suspected that the commentator made a mistake, but since there were 2 of them, I figured there is something fishy here.
After doing some digging in my WordPress Test Enviroment I found the source for the error: the code loops through posts (lines 15-18), and therefore the main WordPress post object no longer contains the same parameters it contained before this loop.
To explain it without going into programming details – after that loop ends, WordPress will have forgotten what the original post was (the post that we wanted to find related posts for).

So the last post in the related posts list is the post that WordPress relates to as the current post (instead of the original).
This is why, each time someone tried to add a comment to post A, WordPress thought they commented on post E (when E is the last post in the related posts list).
To fix this problem I needed to restore the original post object by simply adding a call to the wp-reset-query function after the loop (line 20).
After the change the code should look like this:
<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
}wp_reset_query();
?>
That was only an example of what can happen when forgetting to reset a query inside the loop.
In fact, most of the WordPress hacks loop through posts and you must make sure they end with the wp-reset-query() function call, otherwise strange things might happen..

very well explained. i never understand 100% this function on worpdress. Thanks
Thank you Sergio.
Mine is still messed up, it doesnt reset!
Hi Gus, can you provide a link or paste the problematic code here?
I’m using query_posts on a couple different pages on my blog in order to get the information I want and do pagination, the problem is that whatever I have done has broken the tag cloud and archives on my main blog page here: http://mileswright.com/?page_id=675
When I click on an archive or a tag, it just takes me to the main page with the most recent posts. I tried using the wp_reset_query() after every one of my custom query_posts but that didn’t seem to work. Do you have any ideas of what might be causing it?
I’m using it after the while loop of the loop. Any help would be very greatly appreciated!
Hi Mitchel, did you manage to solve the problem? because, when I click on a tag now, I see a list of posts related to that tag and not the home page..
Nope. I’m really at a loss. I figure that the wp query is getting messed up and I tried using the wp reset but it didn’t seem to fix the problem. I can post some of my code if it would help to see if I’m using it correctly. But I’m really at a loss.
Mitchell, if you want to, you can send the problematic PHP code to my email (or post it here in the comments, whatever you prefer) and I will try to detect the source of the problem.
Nice tip, although I don’t really understand the picture of the blond in the middle of the post? Am I dense?
good jobs
You are a God. This solved a nightmare of an issue on my site!
Thanks Zack, I’m happy to hear it solved a problem in your site.
Great! I wish I could find this article before few hours headache, thanks for sharing
No problem, glad I could help
I’ve ran into an issue where a plugin I built works fine on single pages on a website – but on the homepage it isn’t resetting the loop.
http://stackoverflow.com/questions/4796165/multiple-loops-in-wordpress-second-loop-not-resetting
I’ve tried wp_reset_query and rewind_posts and neither seems to do the trick.
Hi rmlumley, I’ve read the correspondence but it seems the solution they gave you there is the only solution I can provide. Maybe you managed to resolve the problem already?