• The example provided in the section on “Multiple Loops in Action” is great, but it only provides an example for two loops:

    <?php $my_query = new WP_Query(‘category_name=featured&posts_per_page=1’);
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;?>
    <!– Do stuff… –>
    <?php endwhile; ?>
    <!– Do other stuff… –>
    <?php if (have_posts()) : while (have_posts()) : the_post();
    if( $post->ID == $do_not_duplicate ) continue; ?>
    <!– Do stuff… –>
    <?php endwhile; endif; ?>

    How would I do more than two loops. I am setting up a page with up to 4 loops each with a unique category and I am trying to avoid any duplicates at all.

Viewing 6 replies - 1 through 6 (of 6 total)
  • study the bit after ‘Note for Multiple Posts in the First Category’ in here http://codex.ww.wp.xz.cn/The_Loop#Multiple_Loops_in_Action

    (about using an array to collect more than one post id to be excluded)

    Alternatively you can pass the entire $do_not_duplicate array to $wp_query and only entries that match your criteria will be returned:

    <?php query_posts(array('post__not_in'=>$do_not_duplicate));
     if (have_posts()) : while (have_posts()) : the_post();
     ?>

    principle:
    first loop:
    -collect the do-not-dup ids in array;
    second loop:
    -use array to exclude the ids from the loop;
    -collect more do-not-dup ids in the same array;
    third loop:
    -use array to exclude the ids from the loop;
    -collect more do-not-dup ids in the same array;
    fourth/last loop:
    -use array to exclude the ids from the loop;

    Thread Starter James Reyes

    (@jamesreyes)

    Thanks for your response. You have to excuse my ignorance, but I’m just learning PHP yet.

    So if i use this for the second loop,

    <?php query_posts(array('post__not_in'=>$do_not_duplicate));
     if (have_posts()) : while (have_posts()) : the_post();
     ?>

    How do I also restrict by category and number of posts?

    docu for query_posts():

    http://codex.ww.wp.xz.cn/Function_Reference/query_posts

    one possibility:

    <?php
    $args = array(
    'post__not_in' => $do_not_duplicate,
    'posts_per_page' => 5,
    'cat' => 5
    )
    query_posts($args);
     if (have_posts()) : while (have_posts()) : the_post();
     ?>
    Thread Starter James Reyes

    (@jamesreyes)

    Yeah no luck, but I do appreciate the effort.

    I am amazed that I cannot find a single working example of code with 3 or 4 loops with each loop having a unique query (by category and number of posts) while passing do_not_duplicate values along each loop. I’ve scoured the forums and the web and I can’t find one example that doesn’t break.

    If anyone has a working example they can share I would greatly appreciate it.

    Thanks again, everyone!

    you could paste the code of your template into a http://wordpress.pastebin.com/ and post the link to it here – someone might be able to check if and what is wrong.

    you couyld try to use wp_reset_query() between loops.

    what is not working?
    wrong posts?
    still showing duplicates?

    Thread Starter James Reyes

    (@jamesreyes)

    I finally got this to work with the help of a friend of mine. Here is the final product. Feedback, of course, is appreciated:

    http://wordpress.pastebin.com/GEffXe5r

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘More than two multiple loops’ is closed to new replies.