How to reset the query_posts function in WordPress

Knowledge Base on September 10th, 2009 1 Comment

WordPress uses the infamous The Loop to display posts, pages, archives etc. Sometimes depending on the needs (eg. for a specific template for a page in the theme), it might be necessary to use The Loop multiple times within the Template.

Let’s say you are  building a page template that will query_posts first using a predefined string eg. query_posts($my_custom_string). This will reset the original query for the page and later on when you will want to use the functions that correspond to the specific page, they will not be available.

The remedy is to save and reset the corresponding query. The query is by default stored in $wp_query, so you can use <?php $temp_query = $wp_query; ?> before executing custom query searches, and then use <?php $wp_query = $temp_query; ?> to restore it,  just before The Loop that will use the page specific functions.

Update: 22-Feb-2010

If you do not like the old school way described above, you can always revert to the new school way. The WordPress developers have been proactive, as always, and brought us the wp_reset_query function, as noted by one of our visitors.

Tags:

One Response to “How to reset the query_posts function in WordPress”

  1. Andrei says:

    or you could better use wp_reset_query();

Leave a Reply

We stop spam together!

RSS Feed
on facebook
@twitter
footer