
When working with WordPress queries, it may be very useful to be able to clone, store and restore $wp_query. This is exactly what we’re going to see in this recipe.

When working with WordPress queries, it may be very useful to be able to clone, store and restore $wp_query. This is exactly what we’re going to see in this recipe.
With PHP 5, objects can be cloned using the clone operator. The code below is pretty self-explanatory:
// saving the query
<?php $temp_query = clone $wp_query; ?>
<!-- Do stuff... -->
//listing out featured articles
<?php query_posts('category_name=featured&showposts=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do special_cat stuff... -->
<?php endwhile; ?>
// restoring the query so it can be later used to display our posts
<?php $wp_query = clone $temp_query; ?>
This code comes from an article entitled Multiples WordPress loops explained, written by Cristian Antohe at Cats Who Code.
Leave a Comment