Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Avoid Duplicate Post Display with Multiple Loops in WordPress

Are you trying to avoid duplicate post appearing with multiple loops in WordPress?

If you are running multiple WordPress queries to display different sets of posts, then you may come across this issue. Some posts may match more than one loop and would appear twice.

In this article, we will show you how to easily avoid duplicate post display with multiple loops in WordPress.

Avoding duplicate posts when working with multiple WordPress loops

How Duplicate Posts Appear in Multiple WordPress Loops

You can use multiple WordPress loops in a template when creating a custom WordPress theme or a custom page template.

For instance, some users may want to display their recent posts along with their most popular posts. Some users may want to display recent posts followed by posts in different categories.

Now let’s suppose that a post you recently published also matches the criteria for multiple loops. WordPress will display it again unless you exclude it.

Duplicate posts appearing in multiple loops

Now since you are dynamically generating posts for each loop, you cannot manually predict if a duplicate post would appear in a loop.

That being said, let’s take a look at how to easily avoid duplicate posts when dealing with multiple loops in WordPress.

Avoiding Duplicate Posts in Multiple WordPress Loops

For this tutorial, we will show you some sample WordPress code. You can then modify it to your own requirements.

First, let’s recreate the duplicate posts issue.

In the following sample code, we are displaying posts for two categories without avoiding duplicate posts.


<?php
/******  The First Query *******/ 

$first_query = new WP_Query(  array (
'category_name' => 'news'
'posts_per_page'  =>  3
) );
 
// The Loop
if ( $first_query->have_posts() ) {
    echo '<ul>';
    while ( $first_query->have_posts() ) {
        $first_query->the_post();

//display posts
echo '<li>' 
echo the_post_thumbnail( array(50, 50) );
echo get_the_title(); 
echo '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();


/******  The Second Query *******/ 
$second_query = new WP_Query(  array (
'category_name' => 'travel',
'posts_per_page'  =>  3

) );

// The Loop
if ( $second_query->have_posts() ) {

echo '<ul>';
while ( $second_query->have_posts() ) {

$second_query->the_post();
echo '<li>'; 
echo the_post_thumbnail( array(50, 50) );
echo get_the_title(); 
echo '</li>';
    }
echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

As you can see, this code does not check for duplicate posts in either query. It will go on and display duplicate posts if they are found in both categories:

Duplicate posts appearing in multiple loops

Let’s fix this issue.

In order to avoid duplicate posts, we need to temporarily store the data about the posts displayed in the first loop.

Once we have that information, we’ll simply modify our second query to exclude those posts from reappearing.

Here is a sample code that avoids duplicate posts from appearing in the second loop.


<?php
/******  The First Query *******/ 

$first_query = new WP_Query(  array (
'category_name' => 'news'
'posts_per_page'  =>  3
) );
 
// The Loop
if ( $first_query->have_posts() ) {
    echo '<ul>';
    while ( $first_query->have_posts() ) {
        $first_query->the_post();
        
// Store Post IDs in an Array to reuse later
$exclude[] = $post->ID; 

//display posts
echo '<li>' 
echo the_post_thumbnail( array(50, 50) );
echo get_the_title(); 
echo '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();


/******  The Second Query *******/ 
$second_query = new WP_Query(  array (
'category_name' => 'travel',
'post__not_in'  =>  $exclude, // Tell WordPress to Exclude these posts
'posts_per_page'  =>  3

) );

// The Loop
if ( $second_query->have_posts() ) {

echo '<ul>';
while ( $second_query->have_posts() ) {

$second_query->the_post();
echo '<li>'; 
echo the_post_thumbnail( array(50, 50) );
echo get_the_title(); 
echo '</li>';
    }
echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

In the above code, we stored Post IDs in an array $exclude. After that, we added post__not_in argument to our second query to exclude posts that were displayed in the first loop.

That’s all you can now visit your website to see the duplicate posts disappear from the second loop.

No duplicate post found

We hope this article helped you learn how to avoid duplicate post display with multiple loops in WordPress. You may also want to see our comprehensive WordPress theme development cheat sheet for more tips.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

15 CommentsLeave a Reply

  1. Could someone tell me how to do this? I’m really new to this and need a help, please give me a clear example with the post ID, how to store it? Let’s say, mine is 1527.

  2. I’m running 2 loops before loops of a specific category in which I would like to avoid duplicates. So how do a store ID’s in the array from first two loops?

  3. AWESOME!! Thank you so much! And thank you SERGEYVLASOV for that last comment– Worked like a charm for my multiple loops. Hooray!! :)

  4. This method just works fine until both of 2 loops located in one file.
    but when I put the first loop in the header.php and another one in the index.php,
    in_array($post->ID, $do_nit_duplicate) returns null.
    what can I do?

  5. Good afternoon all,

    will this work for my current issue with double display of posts on site? it literally displays a copy under the posts and the 1, 2, -> button…

    the site is I have deactivated re-activated plugins i am literally going nuts.

  6. I think there is a flaw in this algorithm. It can spot no more than 1(one) duplication. So the magic line would look like
     
    $do_not_duplicate[] = $post->ID
     
    and then used as
     
    if(in_array($post->ID, $do_nit_duplicate)) continue;

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.