Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Multiple Loops not working WordPress

  • SOLVED

Hi,

I am trying to run multiple loops on the same page to query two different categories but only one loop works and the other does not (basically the loop that gets read first in my page is the one that works).

Here is my code:

<?php query_posts(array('category_name' => 'events', 'tag' => $post->post_name, 'showposts' => 5)); ?>

<?php while (have_posts()) : the_post(); ?>

<ul>
<li>
<?php the_title(); ?>
</li>
</ul>

<?php endwhile; ?>


and...


<?php query_posts(array('category_name' => 'news', 'tag' => $post->post_name, 'showposts' => 5)); ?>

<?php while (have_posts()) : the_post(); ?>

<ul>
<li>
<?php the_title(); ?>
</li>
</ul>

<?php endwhile; ?>



So one loop is reading from category Events and the other loop is reading from category News.

I have tried reading the codex about Multiple Loops [[LINK href="http://codex.wordpress.org/The_Loop#Multiple_Loops"]][[/LINK]] and doing their suggestions but nothing works thus far

Answers (4)

2011-04-26

Duncan O'Neill answers:

Put this before your second query.


wp_reset_query();
$newQuery = new WP_Query();
$newArgs = array('category_name' => 'news',
'tag' => $post->post_name,
'showposts' => 5);
query_posts($newArgs);


best,

Duncan


Duncan O'Neill comments:

Hi Wordpressing,

if my early solution above worked, please end the question, and vote the prizemoney toward me. thanks, Duncan.

2011-04-26

Christianto answers:

Hi,

Have you tried [[LINK href="http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action"]]create new WP Query for both loop[[/LINK]]?
It wont interfering default post/page query (although this depend on where you put your custom query_post function..)


Wordpressing comments:

Hi Christiano,

I did try that before coming here and I thought for sure that would work but it did not.

Duncan's solution above does work however.




Christianto comments:

Great,

I'm just check if you already done it..

2011-04-26

David Navarrete answers:

$loop1 = new WP_Query(array('category_name' => 'events', 'tag' => $post->post_name, 'showposts' => 5));
<?php while ($loop1->have_posts()) : $loop1->the_post(); ?>


$loop2 = new WP_Query(array('category_name' => 'news', 'tag' => $post->post_name, 'showposts' => 5));
<?php while ($loop2->have_posts()) : $loop2->the_post(); ?>


try with this

greatings


Wordpressing comments:

Hi David,

This solution also works but it appears I had an error in my code initially and went with Duncan's solution which worked well.

I found an example of your solution on a website elsewhere which is what I had tried before coming here.

Thank's for showing up on my thread to you and all (Marvin/Christiano/Duncan)..

Cheers!

2011-04-26

Marvin Shaker answers:

The Answer is Very Simple, I had this Problem Before.
Just Place <?php wp_reset_query(); ?> at the End of the First Loop, or above the Second Loop Hope it Helps.


Wordpressing comments:

Hi Marvin,

This also works, thank you.