Multiple loops

Avoiding repeating posts with multiple loops


I recently completed a project for a popular online magazine. Because of the nature of magazines, the home page was full of custom loops, pulling in specific pieces of content from all over the site. However, it was very possible for content to show up in more than one loop. For example, at the top of the page was the most recent “Feature” and lower down in the page were several loops, pulling in content for posts in certain categories. Because the feature was categorised, just as the rest of the content was, we didn’t want it appearing in that loop, as it had already appeared elsewhere in the page.

The trick to getting around this is creating an array and adding every post that is displayed to that array and then using the post__not_in parameter in WP_Query to ignore any post in that array in future loops (i.e. posts that have already been displayed on the page).

See it in action in the Gist below:

There are two separate loops in this template (lines 1-20 & lines 22-45), though in the original template, there were about 10 or 12 loops, just extending this same practice.

In line 3, you note that we create an empty array called $do_not_duplicate. Then we have our custom WP_Query (lines 4-7), followed by its loop (lines 8-19). The very last thing we do inside the loop (on line 18) is add the post ID to the $do_not_duplicate array.

Now, WP_Query #2 is slightly different because we have added one more argument (line 32), which tells WordPress to ignore any post included in the $do_not_duplicate array, using the post__not_in parameter. This way, this loop cannot display any post which has appeared in a previous loop.

You can then replicate this behaviour for as many loops as you need, allowing you to have a large number of loops, pulling in content from all over your site without ever duplicating a post in any particular view.

Categories: Code & Snippets | Permalink

What next?

Hire me

If you couldn't quite manage this yourself, find it too intimidating, or just don't have the time to do it, you can always hire Dave to do it. Please get in touch so that we can discuss your needs.

Leave a comment

If you have a question, update, or comment about the tutorial, please leave a comment. I try and respond to every comment, though it may take a few days, so please check back soon.

Let a WordPress Expert help you

Do you want to try this, but feel like you need a helping hand, in case something goes wrong? My service, The WP Butler, gives you access to WordPress expertise whenever you need it. Better yet, I'll keep your site backed up, updated and secure, so that you don't have to worry about it. It's all part of the service. Use coupon DIWW and save 15% on all plans.

Visit The WP Butler


Dave has been tinkering with WordPress for many years, and he now shares his WordPress knowledge here on Do It WIth WordPress to help others realise its impressive power. He can also be hired to help with your WordPress needs. Dave, who is British, is married to his best friend, Marti, with whom he has a beautiful daughter, Ellie. When he's not dabbling with WordPress, he's probably eating Triscuits or hummus, watching an indie film or British TV show, spending time with friends or family, or exploring the world.


  1. Hey Dave, just curious – I don’t see that you’re doing it here, so maybe the answer is no – but do you need to reset the query after each loop? (so, using wp_reset_postdata();)

    • Once your loop is complete and you’re finished with it, it’s probably wise to wp_reset_postdata in case you have any code that is relying on using the $post data for the main loop.

  2. Thanks for this excellent solution. I always messed with i++ for different styling but that #do_not_duplicate variable solves that way better.

Leave a Reply