[wp-hackers] The Post Loop

Alex King lists at alexking.org
Wed Jan 19 04:21:33 GMT 2005


The hack I use for getting around this is to store the original query in 
a variable, then re-assign it when I'm done with my other loop. This 
way, you can use all the standard functions that rely on all the globals.

For example:

// going off on my own here

$temp_query = $wp_query;

// do stuff

query_posts('category_name=special_cat&showposts=10');

while (have_posts()) : the_post();
   // Do special_cat stuff.
endwhile;

// now back to our regularly scheduled programming

$wp_query = $temp_query;

HTH.

--Alex

http://www.alexking.org/

Ryan Boren wrote:
> If you need to keep the original query around, you can create a new
> query object.
> 
> $my_query = new WP_Query('category_name=special_cat&showposts=10');
> 
> while ($my_query->have_posts()) : $my_query->the_post();
>   // Do stuff.
> endwhile;
> 
> As you can see, you cannot use the global have_posts() and the_post()
> since they use $wp_query.  Instead, call into your new $my_query object.



More information about the hackers mailing list