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

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

Are you trying to fix duplicate posts displaying 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.

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. You might also help readers find interesting content by showing all the posts in each category.

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.

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 choose the best web design software or see our expert pick of the best drag and drop WordPress page builders.

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

16 CommentsLeave a Reply

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Satriyo says

    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 says

    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. Julie says

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

  5. Pirooz says

    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?

  6. warren says

    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.

  7. sergeyvlasov says

    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.