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.
30 Responses
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!
@Brandon: You’re welcome, glad it helped!
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
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.
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.
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
@Steve: You have to use to wp_list_authors() function
@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="">
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??
@adrian: When using a consequent amount of loops, you should consider using the WP_Query object instead of the classic loop.
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!
Ignore the last question; just me being a noob retard!!
In which file I’ve to put this code to avoid duplicate post publishing?!?
Thanks in advance for your attention and replies…
Trackbacks: