• I have a custom taxonomy only for attachments, with a single option (YES) that can be checked. On the frontpage, I use WP_Query to retrieve all images which are marked, and randomise the output. In fact, I use two queries, first to get one random image and then the rest.

    The output is really strange though – the first 6-7 images are pretty much always the same, randomised amongst themselves and then the rest of the images are shown more or less as they’re listed in the Media Library, with minimal variation. In total, we’re showing about 35 images.

    As far as I can tell, there is nothing wrong with the code – http://pastebin.com/hzGV8HH5. I wonder if there is any limitation on using orderby=rand, or if there would be a different way of calling this?

    I realise this isn’t much data to go on, but I’m pulling my hair out, so thought I’d post this in the off-chance someone might have some ideas..

Viewing 6 replies - 1 through 6 (of 6 total)
  • Just a guess, but try taking out the ‘order’ => ‘ASC’ parameter.

    Thread Starter KS

    (@karl19)

    Thanks a lot for the input! Unfortunately doesn’t change the problem; the order parameter was something I added later to test if it had any effect.

    Moderator keesiemeijer

    (@keesiemeijer)

    Also a guess, but try placing this:

    wp_reset_postdata();

    after the first endwhile; (loop).

    And maybe renaming the second query object differently will help:

    $my_query_two = new WP_Query( $args );
      echo '<div id="thumbnails_wrapper"><div id="outer_container"><div class="thumbScroller"><div class="container">' . "\n";
      while ($my_query_two->have_posts()) : $my_query_two->the_post();
        $do_not_duplicate[] = $post->ID;
            echo '<div class="content"><div><a href="' . wp_get_attachment_url( get_the_ID() ) . '"></a></div></div>' . "\n";
      endwhile;

    Thread Starter KS

    (@karl19)

    @keesiemeijer, thanks for the suggestion! Unfortunately it doesn’t seem to make a difference.

    Most of the initial batch of 8 images that get randomised amongst themselves are unattached, the rest attached to pages generally. But since this is not always the case (1 or 2 are attached to a page), I figured it didn’t have an impact.

    It does seem like this small group of images that always get started with, are the latest to have been uploaded to the media library though. But it does seem to be a bit of an arbitrary limitation to randomise the latest 8, and then the rest. Settings > Reading is set to 8 posts, so it’s not related to that.

    Very odd indeed!

    Moderator keesiemeijer

    (@keesiemeijer)

    It’s possible your theme or a plugin is filtering/altering the query before or after the posts are retrieved from the database. Try it with all plugins de-activated.

    Thread Starter KS

    (@karl19)

    Thanks @keesiemeijer, that might be the case. I’ll have to setup a dev-version of the site and test it out when I get a moment. Thanks for your input.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘WP_Query – issues with rand’ is closed to new replies.