Filter Duplicate Posts in the Loop

As the question arises quite often I’d like to show how I make sure that the content presented, which were output in a loop, not showed up again in a second loop.

WordPress identifies posts and pages via ID, which are created in the database and who also play in the output of the loop a crucial role. All assignments or links based on the ID. Therefore, I save in the first loop (Loop No.1 ) the IDs, which will be output, in an array. This variable was determined in advance as an array $do_not_duplicate = array();.

Loop no.1

ID; // remember ID's in loop
    // display post ...
    the_title();
endwhile;
?>

After we output the above loop with 5 articles, we have in the array 5 IDs, which we can use now . You can view the array with the PHP-function var_dump().

In the next loop no. 2 there should be 15 articles displayed, except the articles with the ID’s already appearing in loop no. 1. I check with this function in_array(),
if the current ID $post->ID is already existing in the array $do_not_duplicate. Only if the ID is not existing ( !in_array() ) in the array, then it will be displayed in loop no. 2.

Loop no.2

ID, $do_not_duplicate ) ) { // check IDs         
// display posts ...
        the_title();
    }
endwhile;
?>

But there is also an alternative, WordPress has a parameter in the query available – post__not_in, see Codex.
Also in this parameter, I use an array and in this query are the IDs not included. Depending on which way you go, are thus provides two different syntax, to avoid duplicate content.

 456,
    'post__not_in' => $do_not_duplicate
    )
);
while ( have_posts() ) : the_post();
    // display posts...
        the_title();
endwhile;
?>

Posted

in

by

Comments

9 responses to “Filter Duplicate Posts in the Loop”

  1. […] en tus Loops. lo cual es bueno para SEO y evitar problemas de validación en ciertos casos.Visto en WpEngineerCompartir:Sponsors Squares.set_spacing(5, 5); Squares.show_squares(1,2); Relacionado […]

  2. Banago Avatar

    Very nice way to work around it. I have been using another approach, but I like this one and I think I will start to use it. Thanks for sharing it!

  3. designerDru Avatar
    designerDru

    This is exactly what I’ve been looking for: 3 separate loops, the first displays the modt recent then sets a $do_not_duplicate to that ID.

    The 2nd loop grabs 6 posts from a specific category, & should not dupe the post from the first loop.

    The 3rd loop grabs 6 posts from a different specific category, & should also not dupe the post from the first loop.

    but no love from the code… If I pass it along would you be willing to take a look?

  4. Alt Design Avatar

    Hi, nice hacks… but what happens to the pagination (eg. wp_navi plugin)?

  5. yeeloon Avatar
    yeeloon

    Hi there… I just stumbled your post after lotsa googling around.. See below if you can help me out – cross posted on (http://wordpress.org/support/topic/382340?replies=1)

    I need some help on the following. Basically, I have 2 different categories called ‘Deals & Offers’ and ‘San Francisco’ where by each categories has several posts under them.

    Now, I’m trying to create a category/page called ‘Deals & Offers in San Francisco’, whereby the page will query and show posts (assuming there are 20 posts under both these categories) that are categorized in ‘Deals & Offers’ and ‘San Francisco’. This category/page will have its own navigation of Next and Previous links when it exceeds more than 5 posts.

    I applied the code below, but everytime I clicked on the Previous/Next button, it keep showing the most recent 5 posts (categorized under Deals & Offers and San Francisco)… 🙁

    How can I possibly show the other remaining 15 posts when I click thru the Next link? Any advice or help would be very much appreciated.. as I have been googling hign and low for this solution, and it seems like nobody has tackle this before?

    Thanks again!


    ?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query(
    array('category__and'=>array(64,112), 'showposts'=>1, '&paged' => $paged));
    ?>

    have_posts()) : $wp_query->the_post(); ?>
    <a href="" rel="bookmark">

  6. yeeloon Avatar
    yeeloon

    Sorry.. here’s the code below. Remove the ‘‘.

    query(
    array(‘category__and’=>array(64,112), ‘showposts’=>1, ‘&paged’ => $paged));
    ? –>


    <a href="” rel=”bookmark”>


  7. Gian Avatar

    Does this work even if each loop is in a different template file? Or they need to be in the same one?

    Becasue I got 2 loops in two template files (header and sidebar) and can’t get it NOT to duplicate.

  8. Legenda Avatar

    Thanks.. I been looking for this code and found out that your solution is simple and better than other.. Tested 3 loop in same file.. not sure working in different file..

  9. Web Design Auckland Avatar

    This doesn’t work for me. According to the codex query_posts shouldn’t be used to create a second loop. But rather a new loop should be started via WP_Query