Top

Daily Tip: Remember to Reset Your Query After a Custom Loop

September 5, 2010

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:

<?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();
?>

Tweet This Post

Comments

Got something to say?

Bottom
Article Archive  |  This site is a historical archive of pressography.com, a WordPress plugin and theme blog that published between 2006 and 2012. It is preserved for reference and is not operated by, affiliated with, or endorsed by its original authors. Nothing here is for sale and no form on this site is active. If you are the original owner and would like this domain returned, please get in touch.