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.

9 Comments

  1. Ben said on August 27, 2009 at 6:53 pm |

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

  2. Mayur Jobanputra said on September 16, 2009 at 9:19 am |

    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?

  3. Abhay (Cone Trees) said on September 16, 2009 at 8:36 pm |

    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.

  4. Jeff Nicholas said on September 18, 2009 at 12:45 pm |

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

  5. Abhay (Cone Trees) said on September 18, 2009 at 7:50 pm |

    Jeff, thanks. Glad it helped you out.

  6. Dave said on December 7, 2009 at 2:46 am |

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

  7. Abhay (Cone Trees) said on December 22, 2009 at 7:31 pm |

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

  8. danny said on March 26, 2010 at 3:52 am |

    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.

  9. Alexander said on July 19, 2010 at 1:51 pm |

    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

Have something to say? Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*