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.
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.
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.
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.
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.
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.
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.
Nick J
Is there a plugin that does this?
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.
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?
Joe
Just what I was looking for – thank you!
Guilherme Alves
Thank you soo much :))) This helps me alot!
Save my day <3
WPBeginner Support
Hi Guilherme,
You are welcome Don’t forget to follow us on Facebook for more WordPress tips and tutorials.
Admin
Julie
AWESOME!! Thank you so much! And thank you SERGEYVLASOV for that last comment– Worked like a charm for my multiple loops. Hooray!!
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?
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.
agus
can you help me?
I have proble with duplicate category in my site
please
#thanks
WPBeginner Support
Seems like a theme specific issue, contact theme developers for support.
Admin
Gabriel
Before iterating over the default loop shouldn’t we use wp_reset_postdata(); ?
Greg
@sergeyvlasov – Thanks that worked for me
tho i changed
$do_nit_duplicate to $do_not_duplicate
Ron Hantman
Checkout this solution which does this task outside of the loop:
http://wordpress.stackexchange.com/questions/61936/removing-duplicate-values-between-two-wordpress-queries/
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;