• Resolved dfwgreg

    (@dfwgreg)


    Hi everyone,
    I’m having a problem with my loops as the second loop is showing duplicate posts. I’ve added this to the first loop: <?php while (have_posts()) : the_post(); ?><?php $displayed = $post->ID;?>and this to the second loop: <?php while ($my_query2->have_posts()) : $my_query2->the_post(); if ($post->ID == $displayed) continue;?>. I’ve even tried offset and it didn’t work.

    Here the full code from index.php: http://pastebin.com/pyPjPbGP.

    All help is greatly appreciated. 🙂

    Thanks and have a great night or day,
    Gregory Schultz

Viewing 2 replies - 1 through 2 (of 2 total)
  • your method would work if there is only one duplicate post; i prefer to work with the array method, using ‘post__not_in’ parameter, which is actually already implemented in the second query:
    <?php $my_query2 = new WP_Query( array( 'post__not_in' => $saved_ids,......

    you simply have to change the first loop to gather the shown post Ids in an array $saved_ids;

    example – change from:

    <?php while (have_posts()) : the_post(); ?>
                                    <?php $displayed = $post->ID;?>

    to:

    <?php $saved_ids = array();
     while (have_posts()) : the_post(); ?>
                                    <?php $saved_ids[] = $post->ID; ?>

    http://codex.ww.wp.xz.cn/The_Loop#Multiple_Loops_in_Action

    Thread Starter dfwgreg

    (@dfwgreg)

    Thanks alchymth, I got it to work!

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

The topic ‘Two loops, second one shows duplicate post’ is closed to new replies.