The Cone Trees network The UX Bookmark UX Quotes UX Jobs in Singapore Nice one yeah!

Solution to query_posts(‘orderby=rand’) not working in WordPress

I had been wondering for a few months by now; why was it that I was able to display random posts in my downloads widget on my local server but not on the live site?

I was using <?php query_posts('orderby=rand'); ?> to do the job. The downloads widget on my website was supposed to display one random post from the download section and did so fine on the local server. But on my live site, instead of displaying a random post, it’d simply default to displaying the most recent post.

I did all I could and tried everything people suggested until I finally discovered, quite by accident, that it was the WP Sticky plugin that was causing the problem . I disabled the plugin and &rand began to work just fine on my live site! I was able to display random download posts just as I wanted to.

So, if you’ve tried everything possible under the sun to randomize your posts using <?php query_posts('orderby=rand'); ?>, and you use WP Sticky, then try disabling the plugin and check whether it makes your randomize post issue disappear.

Let me know if this this help you out too.

  • Ben

    Brilliant – just as my forehead started to bleed from banging the wall google and this post saved me.
    thank you.

  • Thanks for the info. I don’t have sticky installed and I’m still getting this issue. Do you know what part of the sticky plugin is causing this issue?

  • Abhay (Cone Trees)

    You’re welcome Mayur. I don’t know what part of the sticky plugin causes the issue as I’m not into coding.

    Try downloading the plugin and have a look at the code if you haven’t done that already. In case you find out, I’d appreciate you posting a comment with the details.

  • Finally!! Man. That was killing me for 2 days now. You sir, are a genius =)

  • Abhay (Cone Trees)

    Jeff, thanks. Glad it helped you out.

  • Dave

    THANK YOU, THANK YOU, THANK YOU!!!!!! You basically rock. No, you really do rock!! This post made my week!

  • Dave, you’re welcome. Glad it helped you out as well.

  • i’m with ben jeff and dave. you just saved my night, and instead of 10 hours of a bleeding forehead I only had 2.

  • Hello,

    As I did not have the WP Sticky plugin installed I had to look else where and I found it to be another plugin.

    Without going into the technical details here is the function that will ensure that query_posts will work as needed:

    remove_all_filters(‘posts_orderby’);

    Hope this helps,

    Alexander
    Toronto Web Design Expert

  • Brilliant! Thank you..

  • If you a query like:
    query_posts(‘posts_per_page=2&orderby=comment_count&order=DSC’);

    you cannot combine another type of orderby like random + comment count?

    query_posts(‘posts_per_page=2&orderby=randomt&order=DSC’);

  • I had the same problem, but lucky me found this post straight away. I’m not using the sticky-post plugin, but Alexander’s solution worked like a beauty for me.

  • This solution was still pertinent at the time of this comment. Deactivated wp-sticky and orderby=rand started working immediately.

    Thanks!

  • WOW! I pretty was getting crazy about that prob 😉 you resolved it! THX a lot

  • I have the random post issue, but I was not using wp-sticky, Alexander’s solution did the trick for me.
    Thanks!

  • jWo

    Thank you Alexander! You saved me hours of hunting!

  • Jon

    Alexander’s (few commetns above) fix worked for me

  • Brian

    mine doesn’t work

    help please 🙂

    I tried these solutions:

    1)

    ‘rand’,’order’ => ‘ASC’,’cat’ => ‘.$cat_id.’, ‘showposts’ => ‘3’); query_posts( $args );?>

    2)

    3)

  • Thanks,

    In my case W3 Total Cache was the culprit.