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.
Avoid Duplicate Posts in Multiple Loops
Do you want to create a simple PHP array and put post IDs from the first loop in it? While there’s probably a plugin for this, we have created a quick code snippet that you can use to check if post ID is contained in the $ids array and avoid duplicate posts in multiple loops in WordPress.
The end result would be a page with two lists. The first list contains only one post — the most recent post from the ‘feature’ category. The second list will contain X recent posts (as defined in WordPress preferences) except the post that is already shown in the first list. So, once the feature post is replaced with a new one, the previous feature will show up in standard post list section below (depending on how many posts you choose to display and on the post frequency). This technique (or similar) has been used by many in conjunction with knowledge of the Template Hierarchy to create a different look for home.php and index.php. See associated resources at the bottom of this page.