WordPress Multiple Loops
There are certain situations in which using more than one loop in a page or post is essential. One of the most common situations is having a loop INSIDE of another loop. Let’s take a look at how we can accomplish this…
The Code
We’ll start off with the code first. And then explain what everything does second. Please note that this is a page.php template file.
<?php get_header(); ?>
<div id="content">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : ?>
<?php the_post(); ?>
<?php if ($pagename=='news') : ?>
<h1>Latest News Articles</h1>
<?php the_content(); ?>
<?php $newQuery = new WP_Query('category=news'); ?>
<?php if ($newQuery->have_posts()) : ?>
<?php while ($newQuery->have_posts()) : ?>
<?php $newQuery->the_post(); ?>
<div>
<strong><?php the_title(); ?></strong><br />
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">read more »</a>
</div>
<?php endwhile; ?>
<?php else : ?>
<p>Sorry!! There are no news articles here.</p>
<?php endif; ?>
<?php else : ?>
<h1><?php the_title(); ?></h1>
<hr />
<?php the_content(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php get_footer(); ?>
The Explanation
<?php get_header(); ?> <div id="content"> <?php if (have_posts()) : ?> <?php while (have_posts()) : ?> <?php the_post(); ?>
The standard WordPress Loop – The first part of the code as seen above is pretty standard. Nothing out of the ordinary here.
<?php if ($pagename=='news') : ?>
<h1>Latest News Articles</h1>
<?php the_content(); ?>
<?php $newQuery = new WP_Query('category=news'); ?>
<?php if ($newQuery->have_posts()) : ?>
<?php while ($newQuery->have_posts()) : ?>
<?php $newQuery->the_post(); ?>
<div>
<strong><?php the_title(); ?></strong><br />
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">read more »</a>
</div>
<?php endwhile; ?>
<?php else : ?>
<p>Sorry!! There are no news articles here.</p>
<?php endif; ?>
The second and nested loop – Alright. This is where things might get a little tricky. Firstly, we’ll check to see what page we’re requesting. If the page name is ‘news’ then we’ll execute a custom set of instructions.
<?php $newQuery = new WP_Query('category=news'); ?>
Defining a new loop – As seen above, the first part in setting up another wordpress loop inside of an already running wordpress loop is to put the new loop object into a variable. As noted in the code below, we’ll call this new variable called $newQuery. This variable obviously can be named whatever you choose.
We also will be using the WP_Query object to build our new loop. Using query_posts does not work in this situation.
<?php if ($newQuery->have_posts()) : ?> <?php while ($newQuery->have_posts()) : ?> <?php $newQuery->the_post(); ?
Setting up a new loop - Next, we just have to use the information in the variable to build a new loop. As seen from the above code, this is setup exactly like a normal wordpress loop but, references the $newQuery variable.
<div> <strong><?php the_title(); ?></strong><br /> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">read more »</a> </div>
Displaying the content – Lastly, we have to display the information using standard wordpress template tags. Note that these tags do NOT reference the $newQuery variable. The code for presenting a loop ALWAYS remainds the same.
Conclusion
And that’s it!! As we can see, this technique is very powerful and with some creative thought can be used in many, many different situations. This is also yet another step in using WordPress as a full blown CMS.




November 11th, 2009 at 12:07 pm
Hi, thanks for this post it really helped me to connect a jQuery slider to the WordPress backend for easy editing by my client.
Just a note, to limit the query to the appropriate category I had to use the following which is slightly different than above (WP v2.8):
The query uses ‘category_name=…’ instead of ‘category=…’
Thanks again!
John
November 11th, 2009 at 12:08 pm
Code got taken out, hopefully this works:
November 11th, 2009 at 12:10 pm
Grrrr.
Add the correct opening and closing php tags to this line:
$cat_PortfolioSlider = new WP_Query(‘category_name=Portfolio_Slider’);
February 28th, 2010 at 5:02 am
you will have a problem with this method, as the second loop will overwrite the global $post variable – this could cause strange things to happen.
March 4th, 2010 at 2:39 pm
Interesting. How would you suggest it then?