Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Avoid Duplicate Post Display With Multiple Loops in WordPress

If you are using multiple WordPress queries to display different sets of posts, then you may come across duplicate content. This is because some posts might match more than one loop and appear twice.

WordPress doesn’t come with a built-in option to avoid duplicate posts in multiple loops. However, in our 15+ years of experience handling different WordPress sites, we’ve learned how to avoid this issue. So, we’ve created a custom code snippet that you can add to your site and avoid duplicate posts being shown in different loops.

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

When creating a custom WordPress theme or a custom page template, you may sometimes need to use multiple WordPress loops.

For example, you may want to show your recent posts next to your site’s most popular posts. By showing all the posts in each category, you might also help readers find interesting content.

In all of these examples, a single post can match the criteria for multiple loops. When this happens, WordPress will display duplicate content.

This duplicate content can make your site look messy and unprofessional. It also takes up onscreen space without adding value.

Since you are dynamically generating posts for each loop, you can’t manually predict if a duplicate post will appear in multiple loops.

With that being said, let’s look at an easy way to avoid duplicate posts when dealing with multiple loops in WordPress.

Avoiding Duplicate Posts in Multiple WordPress Loops

For this guide, we will show you some sample WordPress code that causes the duplicate post error and then share a code snippet that fixes the problem.

When creating a WordPress child theme or custom template, your code may be completely different. However, you can use our code snippet as a starting point and then modify it to fit your own website.

First, let’s create a duplicate post issue. In the following sample code, we are displaying all the posts in the ‘travel’ category and all the posts in the ‘news’ category without avoiding duplicate posts:

/******  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.

If a post belongs to both the ‘news’ and ‘travel’ category, then it will appear twice, as you can see in the following image.

How to avoid duplicate post display with multiple loops in WordPress

Let’s fix this issue.

In order to avoid duplicate posts on your WordPress blog, you’ll need to temporarily store the data about all the posts displayed in the first loop.

Once you have that information, you can modify the second query to stop duplicate posts from appearing in the second loop:

/******  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 are storing Post IDs in an array called $exclude. After that, we’re adding the post__not_in argument to the second query, which will exclude posts that were displayed in the first loop.

If you’re not familiar with adding code snippets to your site, then an easier way is by using WPCode. It is the best code snippet plugin for WordPress that helps you add custom code without risking breaking your site.

First, you’ll need to install and activate the WPCode plugin. If you need help, then please see our guide on how to install a WordPress plugin.

Upon activation, you can go to Code Snippets + Add Snippet from the WordPress dashboard and then click the ‘Add Your Custom Code (New Snippet)’ option.

Add a new custom code snippet in WPCode

After that, you can paste the custom code into the Code Preview area and enter a title at the top.

You’ll also need to select the ‘Code Type’ as PHP Snippet by clicking the dropdown menu.

Enter custom code snippet

After entering the code, simply scroll down to select the Insertion section.

Here, you can use the default setting of ‘Auto Insert’ and let the plugin automatically add the code to your site.

Insertion methods for snippets in WPCode

Once you are done, simply save the snippet and activate it.

For more details, please see our guide on how to add custom code in WordPress.

Now, if you visit your WordPress website, you’ll see that the duplicate posts have disappeared.

Removing duplicate posts when using multiple posts in WordPress

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 guide on how to disable any number of posts in WordPress loop and how to make sticky posts in WordPress.

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. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

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. Nick J

    Is there a plugin that does this?

  2. Satriyo

    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.

  3. Gaurav

    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?

  4. Joe

    Just what I was looking for – thank you!

  5. Guilherme Alves

    Thank you soo much :))) This helps me alot!
    Save my day <3

  6. Julie

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

  7. Pirooz

    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?

  8. warren

    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.

  9. agus

    can you help me?
    I have proble with duplicate category in my site

    please
    #thanks

  10. Gabriel

    Before iterating over the default loop shouldn’t we use wp_reset_postdata(); ?

  11. Greg

    @sergeyvlasov – Thanks that worked for me

    tho i changed

    $do_nit_duplicate to $do_not_duplicate

    ;)

  12. sergeyvlasov

    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.