Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Displaying Multiple Loops Without Showing Duplicate Posts WordPress

  • SOLVED

Hi all!

Here's what I'm trying to do:

I need to display seven (7) images that are assigned to a custom post type called 'animals'. Each image needs to be displayed in a different way which is why I need to use separate loops.

I think the only way I can do this is to assign the displayed post ID's to an array and check if they've been used before. I don't want duplicate images to appear.

I can get it to work for two loops but problems start once I add a third loop. It starts to display duplicate images. I'm sure it's because I'm not assigning the ID's into the array correctly as sequential loops are triggered which is why I need your help.

I also need the images to display randomly (as you'll see in my arguments array).

Here's what I have so far:



<?php // First Image Loop
$args=array(
'post_type' => 'animals',
'orderby' => 'rand',
'posts_per_page' => 1
);

$ids = array(); // Set variable as array

$query = new WP_Query($args);
while($query->have_posts()) : $query->the_post();

?>

[THE FIRST IMAGE IS DISPLAYED HERE]

<?php
$ids[]= $post->ID; // Assign the posts ID to the array
endwhile;
wp_reset_query();
?>

<?php // Second Image Loop
$args=array(
'post_type' => 'animals',
'orderby' => 'rand',
'posts_per_page' => 1
);

$query = new WP_Query($args);
while($query->have_posts()) : $query->the_post();

if (!in_array($post->ID, $ids)) {

?>

[THE SECOND IMAGE IS DISPLAYED HERE]

<?php } endwhile;
wp_reset_query();
?>




How can I add a third, fourth, fifth, etc. loop that remembers the previous post ID's so as not to show them again?

Answers (3)

2011-09-11

Ivaylo Draganov answers:

Hello,

I think you ought to add the IDs from each loop to the array. Place this line before the end of each loop (before endwhile;):

$ids[]= $post->ID; // Assign the posts ID to the array


I see it in the first loop but not in the second one. Maybe you just missed it.


beowulf comments:

It looks like you're right about that. I should have added that at the end of my other loops.

But even though I have 7 loops, the page doesn't always display 7 posts.

It's always a different number of posts displayed when I refresh the page. I have 10 custom posts so it should definitely be able to display 7 different posts but when I refresh the page, it sometimes shows 3 and on another refresh it shows 5 or 6. So strange.

Any idea why some loops decide not to display anything at all?


Ivaylo Draganov comments:

<blockquote>Any idea why some loops decide not to display anything at all?</blockquote>

Because as Kannan pointed out that's the wrong approach to query for random posts. You get different number of posts each time because each of your loops queries for 1 posts only and if it matches one of the previous ones nothing would come out of the query.

Use the approach that Kannan suggests - query for the number of posts you need ordered by 'rand' and the use some additional PHP code to display them differently. You could set them up as an array and call each image or you could use a counter inside the loop to change the appearance based on the order of the image. Something like this:
[[LINK href="http://pastebin.com/GZzXCJJ6"]]http://pastebin.com/GZzXCJJ6[[/LINK]]


beowulf comments:

Yes, Kannan is totally correct that I should approach this from a different way. Your code is definitely getting me on the right track too! I'm going to give it a try now.

2011-09-11

Kannan C answers:

i don't understand why are you querying several times. You can get all images randomly in a single query and even if you want it to display in different positions of a page, you can store it an variable as array and display it as for ex: $image[0], image[1],...

<?php // First Image Loop

$args=array(

'post_type' => 'animals',

'orderby' => 'rand',

'posts_per_page' => 5

);



$ids = array(); // Set variable as array



$query = new WP_Query($args);

while($query->have_posts()) : $query->the_post();

$images[] = get_the_post_thumbnail( $post_id, $size, $attr );//if you want to display in different location of a page

?>



[ALL IMAGE IS DISPLAYED HERE RANDOMLY]



<?php


endwhile;

wp_reset_query();

?>


beowulf comments:

I thought there would be a cleaner way to do it. Thanks a lot for your suggestion.


Kannan C comments:

You are always Welcome.

2011-09-12

ej_emman answers:

Hello beowulf,

I am thinking of the things you want to happen based on your question. Everyones suggestion is right. But I'm trying things you really desire. Hope this help..
<blockquote>
Each image needs to be displayed in a different way
</blockquote>


<?php // suggestion
$args=array(
'post_type' => 'animals',
'orderby' => 'rand',
'posts_per_page' => 7
);

$query = new WP_Query($args);

while($query->have_posts()) : $query->the_post();


//Your reference in display image differently
if($post->ID=='1')// your post id equivalent
{
echo get_the_post_thumbnail(); //or place some another attributes to your image
//do other stuff
}
else if($post->ID=='2')
{
echo get_the_post_thumbnail(); //or place some another attributes to your image
//do other stuff
}
else if($post->ID=='3')
{
echo get_the_post_thumbnail(); //or place some another attributes to your image
//do other stuff
}
else if($post->ID=='4')
{
echo get_the_post_thumbnail(); //or place some another attributes to your image
//do other stuff
}
else if($post->ID=='5')
{
echo get_the_post_thumbnail(); //or place some another attributes to your image
//do other stuff
}else if($post->ID=='6')
{
echo get_the_post_thumbnail(); //or place some another attributes to your image
//do other stuff
}

else
{
echo get_the_post_thumbnail(); //or place some another attributes to your image
//do another fun stuff
}

endwhile;

wp_reset_query();
?>