Post Pic

How to: Use two (or more) loops without duplicate posts

Today, my friend Mehmet asked me if I know how to use multiples loops without having the duplicate posts problem. There’s probably many other ways to achieve the same effect, but here’s a pretty nice way to avoid duplicate posts in two or more loops.

To avoid post duplication, I started by creating a php array, and adding the IDs of the posts I get in the first loop to that array:

<h2>Loop n°1</h2>
<?php
$ids = array();
while (have_posts()) : the_post();
the_title();
?>
<br />

<?php $ids[]= $post->ID;
endwhile; ?>

Now, the second loop: I have used the php function in_array() to check out if a post ID is contained in the $ids array. If the IDs isn't contained in the array, we can display the post as it wasn't displayed on the first loop.

<h2>Loop n°2</h2>
<?php
query_posts("showposts=50");
while (have_posts()) : the_post();
if (!in_array($post->ID, $ids)) {
  the_title();?>
  <br />
<?php }
endwhile; ?>

That's done: No duplicated post!
Alternatively, it is possible to transform the $ids array to a comma separated string and use in along with the exclude parameter of the query_post() function.

Related Posts

Related Posts

No related posts.

30 Responses

Dec 20 2008 17:57

This could not have arrived in my feed reader at a more opportune time! I’m working on a blog design that requires two loops on the home page and this is a perfect solution – thanks!

Dec 20 2008 18:07

@Brandon: You’re welcome, glad it helped!

Jan 07 2009 23:44

Hi there,

My home page (http://www.expressionsbymotti.com/) has two head sections called ‘Featured’ and ‘Headings’. When I click on ‘Next’ page I keep getting page one with these two sections.

The blog is stuck on page one.

Can this tutorial solve this problem?

Thank you,
Motti

Jan 19 2009 17:24

There’s a better way with less overhead.
Do the first part like you have, but change the 2nd query object to be:

$query = query_posts(array(’showposts’=>5,’post__not_in’=>implode(‘,’,$ids));
or, if you need a brand new query so as not to mess with the current query scope
$query = new WP_Query(array(’showposts’=>5,’post__not_in’=>implode(‘,’,$ids));

That way you’ll exclude the posts you don’t want at the time of the query instead of pulling a huge overhead of data you don’t need.

Apr 06 2009 11:24

A ran in to a problem here. I want to display posts in 3 blocks from same category & without any duplicate in each block.
I inserted first chunk of your code & added:
query_posts(“showposts=4&category_name=headlines”); within php tags. It showed content from right category with given number of post.
Now in 2nd block when I insert your code (the 2nd loop) and add category after number of posts like:
query_posts(“showposts=50category_name=headlines”)
It doesnt work & show nothing.
Please help me solve this problem.

May 31 2009 00:26

hi,
i’m trying to show “recent contributors” list for my site. i’m thinking the code you’ve shared here might put me on the right track, but i’m still struggling.
do you know of a simple way to make a list of the most recent authors? this could either use the wordpress “author” or could get the values from a custom field for each post, whatever works.
I hope you can help or point me in the right direction.
Thanks,
Steve

May 31 2009 07:31

@Steve: You have to use to wp_list_authors() function :)

Jun 05 2009 18:12

@Sajid

I struggled with this as well; maybe this helps a bit. This works fine for me. Now I just gotta figure out how to make use of the sticky feature.

<div id="post-" class="">

Meet the staff

<div class="">


Jun 19 2009 00:44

nice post, but what if i try to use 3 or 4 or more loops???
i tried using this on 3 loops, but the 3rd loop shows the same content as the 2nd..

any ideas??

Jun 19 2009 07:44

@adrian: When using a consequent amount of loops, you should consider using the WP_Query object instead of the classic loop.

Aug 04 2009 13:36

Thanks, this seems to work great. I have one question: currently the code displays the title as text above the post. How can I convert that to a link within h3 tags for styling? I can’t seem to find the right syntax??!! Cheers!

Aug 04 2009 14:35

Ignore the last question; just me being a noob retard!!

Sep 23 2009 14:41

In which file I’ve to put this code to avoid duplicate post publishing?!?
Thanks in advance for your attention and replies…

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required