0

I use a random wp_query to load 3 posts from over 600 posts in total with this query.

        <?php
            $args = array(
                'post_type' => 'post',
                'cat' => 1,
                'orderby' => 'rand',
                'posts_per_page' => 3
            );
            $all_posts = new WP_Query( $args );

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

                get_template_part( 'content' );

            endwhile; 

        ?>
        <?php wp_reset_postdata(); ?>

Works great, but it seems for a lot of people a lot of the same images popup in the random display over and over again. So it seems the random isn't really random. At least it doesn't feel this way. Is there a way to force Wordpress to skip random displayed images and load new ones? Or is it out of my hands in this case? I can't really find an answer online.

Thanks in advance!

  • Unfortunately that is the joy of random ordering. :-) – Pieter Goosen Jun 9 '15 at 17:49
  • I understand :) But the problem is I use this for voting and if a random image popup like 10 out of 20 times people start asking questions. That I also understand :) – AKNL Jun 9 '15 at 19:08
1

I wanted to point out that sometimes in the case of random it's not the code at all, if you're using a host that does auto cache or are using a cache plugin, it's possible the page or the query itself is getting cached and therefor the same results will show.

Fixes for this issue would be to have the host or the plugin turn off caching completely on the page that has the query.

  • Hi ScottG that is very correct and I experienced big issues with this. My host (Media Temple) refuses the turn off server side caching because they say it is part of the wordpress hosting infrastructure. I have a work around where I generate a guid behind my urls in the voting proces to force new random images. This works fine, but maybe it is part of the 'random' problem. – AKNL Jun 10 '15 at 7:03
0

You aren't really talking about forcing WordPress to be more random; you are talking about forcing the database server to be more random. WordPress just pushed ORDER BY RAND() through to the server. And yes, generated "random" isn't truly random, it is pseudo-random but for all intents and purposes it should be close enough. The only thins I can think of that might help would be to push 'ignore_sticky_posts' => true through your argument array.

$args = array(
  'post_type' => 'post',
  'cat' => 1,
  'orderby' => 'rand',
  'posts_per_page' => 3,
  'ignore_sticky_posts' => true
);

'ignore_sticky_posts', somewhat inexplicably from my perspective, defaults to false meaning that sticky posts always get shuffled to the top unless an argument is explicitly passed to the query. That is backwards to me, but so it is. At any rate, if you have a lot of sticky posts that could explain why you are seeing the same few things over and over.

  • Thanks s_ha_dum for your thoughts. I don't use any sticky posts on my website. – AKNL Jun 9 '15 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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