In coding WordPress plugins and themes, I have often found it necessary to create secondary WordPress loops in the code to show a list of articles or posts on the site that wouldn’t normally appear on the page used to display them. For example, on the homepage of this site, I list articles from three different blog categories in three different lists. The way in which this is accomplished is relatively simple and straightforward.
First, you have to create a new instance of the WP_Query object from WordPress. Due to some complications with the WordPress code, you will need to store the original $wp_query variable in a $temp variable, and override it with your own. You would do this with something like the following code:
<?php $temp = $wp_query; $wp_query = null; $wp_query = new WP_Query(); ?>
This constructs a new WP Query object for you which you can then use for subsequent calls in the loop. Once you’ve constructed this object, you will want to define your query variables and run your query.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = 'cat=5&paged=' . $paged; // Query to pull posts from category ID 5
$wp_query->query($query); // Run the query on the $newposts object.
?>
Now that you have retrieved the necessary posts from your blog, you can begin your secondary loop. The format is slightly different however, in that you should run the have_posts() methods on the new object, rather than as WP functions as is seen below.
<?php
if ($wp_query->have_posts()) :
while ($wp_query->have_posts()) :
$wp_query->the_post();
?>
… regular loop style WP code such as…
<li>
<a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title(); ?>"
><?php the_title(); ?></a>
</li>
… then close the loop.
<?php endwhile; endif; ?>
If necessary you can also add paging controls with the following:
<div class='navigation'>
<div class='alignleft'>
<?php next_posts_link('« Older Entries') ?>
</div>
<div class='alignright'>
<?php previous_posts_link('Newer Entries »') ?>
</div>
</div>
Finally, you need to restore the original $wp_query variable with what you stored in $temp.
<?php $wp_query = null; $wp_query = $temp; ?>
I’d love to hear your feedback on this or if you have any suggestions to improve it. Feel free to comment.
2 Responses to “Secondary WordPress Loops”
Feedback
Related Entries
- FormBuilder WordPress Plugin
- WordPress
- WordPress and Multiple Installations of WordPress
- SendFeed WordPress Plugin
- New FormBuilder WordPress Plugin!
- Daily Jokes for Your Website
- AmazonFeed v1.9 Released
- Beware of Thieves like AmazonPress
- WordPress Plugin: Related Products from Amazon.com
- Installation Instructions
Links:
- ReputationDefender
- Web Hosting Canada hosting packages starts at $3.95/m. 24/7 toll free support.
Recommended:
Other Information:
Programming Related
Articles we've written related to the topic of PHP Programming.
- Beware of Thieves like AmazonPress
(April 22nd, 2010 – 2 Comments) - Paginated Navigation Bar
(December 21st, 2009 – 2 Comments) - Wicked Cool PHP – Review
(November 23rd, 2009 – One Comment) - PHP Object Unit Testing
(June 5th, 2009 – Comment on This) - Link Directory Plugin Work
(April 8th, 2009 – Comment on This)
Website Development Tips
Tips and strategies related to the development of great websites.
- Aptana and Eclipse Mouse Click Problems
(January 21st, 2010 – Comment on This) - Captcha Insanity?
(June 18th, 2009 – One Comment) - PHP Object Unit Testing
(June 5th, 2009 – Comment on This) - FormContact 1.1 with PHP5 Support
(June 24th, 2008 – 2 Comments) - Website Subversion Backup
(April 8th, 2008 – One Comment)
General Information & Resources
General information and resources from WarkenSoft Productions.
- Beware of Thieves like AmazonPress
(April 22nd, 2010 – 2 Comments) - Aptana and Eclipse Mouse Click Problems
(January 21st, 2010 – Comment on This) - Wicked Cool PHP – Review
(November 23rd, 2009 – One Comment) - New Webmaster Tool – The Regular Expression Editor
(June 15th, 2007 – Comment on This) - Where to Start with a Website
(March 19th, 2007 – Comment on This)



![The impact of secondary tasks on multitasking in a virtual environment [An article from: Acta Psychologica] The impact of secondary tasks on multitasking in a virtual environment [An article from: Acta Psychologica]](http://ecx.images-amazon.com/images/I/51ME064T9CL._SL75_.jpg)



January 22nd, 2010 at 9:55 am
I guess I don’t understand why one would want to reuse the $wp_query variable. Why not just create a new variable such as $myNew_query = new WP_Query(); and just leave the original $wp_query as is and run any secondary loops on their own named query objects?
`Five Most Recent Articles on…`
`
“
`have_posts()) : $recentPosts->the_post(); ?>`
`<a href="” rel=”bookmark”> by `
“
“
That way in the code the variable naming can make it is clear what this particular snippet is doing.
January 22nd, 2010 at 9:56 am
hmmm…. how to make the code show up as text?