Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Avoid Duplicate Post Display with Multiple Loops in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Avoid Duplicate Post Display with Multiple Loops in WordPress

Recently one of our users ran into the issue of having duplicate post while developing a custom theme. What he was trying to do was show the most recent post on the homepage, and then show a list of random posts in a separate loop on the same page. The issue was that the most recent post would sometime be duplicated in the random post loop. In this article, we will show you how to avoid duplicate post display when using multiple loops in WordPress.

The trick to avoiding duplicate post display is to store the post ID from the first loop, then check against that in the second loop. Here is how you do it. Your first loop’s code need to look like this (notice the magic line):

<?php $my_query = new WP_Query('category_name=featured&posts_per_page=1');
  while ($my_query->have_posts()) : $my_query->the_post();

  $do_not_duplicate = $post->ID; //This is the magic line

    <!-- Do stuff... -->
<?php endwhile; ?>

Now that we have stored the post ID from the first loop under $do_not_duplicate variable, lets add a check for that in our second loop. Your second loop code should look something like this:

<?php if (have_posts()) : while (have_posts()) : the_post(); 

  if( $post->ID == $do_not_duplicate ) continue; //This is the Magic Line

   <!-- Do stuff... -->
  <?php endwhile; endif; ?>

As long as you add those two lines in there, your posts will NOT replicate. To all new theme designers, we hope this helps.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. Julie says:

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

  2. 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?

  3. 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.

  4. agus says:

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


  5. Gabriel says:

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

  6. Greg says:

    @sergeyvlasov – Thanks that worked for me

    tho i changed

    $do_nit_duplicate to $do_not_duplicate


  7. Ron Hantman says:
  8. 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;

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.