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.


 'news'
'posts_per_page'  =>  3
) );
 
// The Loop
if ( $first_query->have_posts() ) {
    echo '
    ‘;
    while ( $first_query->have_posts() ) {
    $first_query->the_post();

    //display posts
    echo ‘


  • echo the_post_thumbnail( array(50, 50) );
    echo get_the_title();
    echo ‘
  • ‘;
    }
    echo ‘

‘;
} 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 ‘

    ‘;
    while ( $second_query->have_posts() ) {

    $second_query->the_post();
    echo ‘

  • ‘;
    echo the_post_thumbnail( array(50, 50) );
    echo get_the_title();
    echo ‘
  • ‘;
    }
    echo ‘

‘;
} 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.


 'news'
'posts_per_page'  =>  3
) );
 
// The Loop
if ( $first_query->have_posts() ) {
    echo '
    ‘;
    while ( $first_query->have_posts() ) {
    $first_query->the_post();

    // Store Post IDs in an Array to reuse later
    $exclude[] = $post->ID;

    //display posts
    echo ‘


  • echo the_post_thumbnail( array(50, 50) );
    echo get_the_title();
    echo ‘
  • ‘;
    }
    echo ‘

‘;
} 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 ‘

    ‘;
    while ( $second_query->have_posts() ) {

    $second_query->the_post();
    echo ‘

  • ‘;
    echo the_post_thumbnail( array(50, 50) );
    echo get_the_title();
    echo ‘
  • ‘;
    }
    echo ‘

‘;
} 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.