Remember to Reset Your Query After a Custom Loop

If you’re implementing some WordPress hacks you’ve found or calling a custom loop to show related posts after your post, make sure to reset the query. You can do this using wp_reset_query. It will destroy the previous query used on a custom loop. If you don’t use the reset, it’s possible that some strange things might happen on your page, such as the wrong comments showing for your post.

Here’s an example from the codex of how to use it with the reset shown at the end:

1
2
3
4
5
6
7
<?php
query_posts('showposts=5');
if (have_posts()) : while (have_posts()) : the_post();
    ?><a href="<?php the_permalink() ?>"><?php the_title() ?></a><br /><?php
endwhile; endif;
wp_reset_query();
?>

Comments (2)

Participate