WordPress PHP - Post duplicates in 2 loops

By Lewis Wolfe, Saturday, May 3rd, 2008

A quick and simple way to knock out duplicates if you have 2 Loops on the page

The first loop will look something like this:-

$duplicates = array();
$my_query = new WP_Query(’category_name=Car Insurance&showposts=8′);

while ($my_query->have_posts()) {

$my_query->the_post();

$duplicates[] = $post->ID;

//print out the 1st loop posts
}

so this gives you an array of post ids to check against the posts of the second loop, and inside this you use the condition

if (!in_array(get_the_ID(),$duplicates)){
// print out the 2nd loop posts
}

No responses so far

Leave a comment

Want a gravatar?
gravatars
Go to gravatar.com and upload your preferred avatar.

Your comment *

RSS feed for comments on this post. TrackBack URI