logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$5
Multiple Loops not working

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 and doing their suggestions but nothing works thus far

This question has been answered.

Wordpressing | 04/26/11 at 2:10am Edit


(4) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    04/26/11
    2:23am
    Duncan O'Neill says:

    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

    • 04/26/11 4:41pm

      Duncan O'Neill says:

      Hi Wordpressing,

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

  • avatar
    Last edited:
    04/26/11
    3:47am
    Christianto says:

    Hi,

    Have you tried create new WP Query for both loop?
    It wont interfering default post/page query (although this depend on where you put your custom query_post function..)

    Previous versions of this answer: 04/26/11 at 3:47am

    • 04/26/11 4:04am

      Wordpressing says:

      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.



    • 04/26/11 4:18am

      Christianto says:

      Great,

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

  • avatar
    Last edited:
    04/26/11
    7:22am
    David Navarrete says:

    $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

    • 04/28/11 8:02am

      Wordpressing says:

      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!

  • avatar
    Last edited:
    04/26/11
    10:12am
    Marvin Shaker says:

    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.

    • 04/28/11 8:00am

      Wordpressing says:

      Hi Marvin,

      This also works, thank you.

This question has expired.



Wordpressing voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.