Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the following to display random images:

<?php $new_query = new WP_Query('&showposts=24'); ?>
         <?php while ($new_query->have_posts()) : $new_query->the_post(); ?>
         <?php $args = array (
            'post_type' => 'attachment',
            'numberposts' => 1,
            'orderby' => rand,
            'status' => 'publish',
            'post_mime_type' => 'image',
            'parent' => $post->ID
         ); ?>
         <?php $attachments = get_posts($args);
            if ($attachments) {
                foreach ($attachments as $attachment) {
                    echo '<li>';
                    echo wp_get_attachment_link($attachment->ID, 'thumbnail', true, '');
                    echo '</li>';
                }
            };

            endwhile;

But I'm trying to avoid duplicates. Any ideas?

Thanks!

share|improve this question
    
How are you getting the duplicates, do you mean the same looking image with a diff file name? –  Wyck Jan 3 '12 at 15:27
    
Wyck I seem to be getting the same image more than once, which makes sense I guess due to 'numberposts' being set to rand, which I want, but I don't want the same image twice. –  jw60660 Jan 3 '12 at 22:12
add comment

1 Answer

up vote 1 down vote accepted

One way to do this would be to use post__not_in because you are using 'orderby' => rand,.

You will need to throw the post or attachment ID ( I suppose you can also use something else) into and array in the first loop and use that array in as a second query parameter, in essence you are doing 2 loops.

I don't have to ability to test the actual code but the logic would be along the lines of;

    $remove_duplicates = array(); //create an array to hold post id's (or whatever you use)

    $new_query = new WP_Query('&showposts=24&orderby=rand'); //add your other queries params here

    while ($new_query->have_posts()) : $new_query->the_post(); // start first loop

    $remove_duplicates[] = $post->ID ?>  // throw duplicate post Id's into an array

    endwhile;  // end first loop

    // start second loop for your output using the array

      $args=array(
                 'numberposts' => 1,
                 'orderby' => rand,
                 'status' => 'publish',
                 'post_mime_type' => 'image',
                 'parent' => $post->ID
                 'post__not_in'=> $remove_duplicates ); // this is the important part to remove the duplicates.

      // rest of your output.

Sorry I was unable to test this, if it doesn't work , another option would be to just use a native PHP function like array_unique to remove the duplicates in the array before output.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.