Support » Fixing WordPress » Add random sort option to query_posts

  • ryangreenberg


    There are a bunch of plugins offering random this or that with respect to posts, but the most versatile solution is to have random posts going into The Loop. I used the solution given by Kafkaesqui here to get a set of random posts. Works great, but it would be nice to add an option to the query_posts function to minimize the effort.

    Already you can write:
    <?php query_posts('showposts=5&order=ASC'); ?>

    How about adding:
    <?php query_posts('showposts=5&order=RAND'); ?>

    I am still learning the setup of the WP files. Once I get things down I might try to submit the change myself, but I thought I’d submit it here as a suggestion. Anyone else think this addition would be useful or worthwhile? For those looking for a solution now, do check out the link above.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator Samuel Wood (Otto)


    In wp-includes, query.php, look for this line:
    $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order');

    And change it to this:
    $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order', 'rand');

    Also find this just below that:

    if ( 'menu_order' != $orderby )
    $orderby = 'post_' . $orderby;

    And change it to this:

    if ( 'menu_order' != $orderby && 'rand' != $orderby)
    $orderby = 'post_' . $orderby;
    if ('rand' == $orderby)
    $orderby = 'RAND()';

    After making those two changes, then you can do this:
    <?php query_posts('showposts=5&orderby=rand'); ?>

    I added a ticket to trac for this sort of change, hopefully it’ll get into WordPress soon:




    ryan, you dont need to do all that other stuff in that link. Just this:

    shuffle(query_posts("all your other crap in here"));

    Moderator Samuel Wood (Otto)


    whooami: That won’t work. query_posts() doesn’t return an array, so your shuffle would do nothing.

    In theory, you could pull all the posts, shuffle the posts array, and then cut off all but the first X posts, but that’s way overkill and adds a lot more strain to the database by pulling all your posts to begin with.

    The solution I posted above is likely the best answer. It makes the database give you the posts you want in a random order.




    uh, yes it does otto. Its already been proven to work.

    I dont typically suggest things over and over unless I’ve 1. tested it, and 2. seen it work for someone else

    (refresh the page and WATCH it randomize the order)





    code for that=

    <?php shuffle(query_posts('author=1')); ?>

    and a normal loop, after.

    and I wasnt ever suggesting that this was ‘Better’ than your method.

    Your method, however, requires hacking the core — I’m pretty sure I can find at least one post where even you have spoke out against doing that.

    Moderator Samuel Wood (Otto)


    whoami: Okay, I see why it works now, but still, that method will only shuffle the posts around on the page. It won’t get older posts that were not on that page.

    If you have it set to display the latest 10 posts, for example, then shuffling like that is only going to move those ten around. You’ll never get any posts from the next page. It’s not really a random selection of all your posts, it’s just reordering the posts you already had.

    And yes, hacking the core is bad, *except* when there’s no other choice. Currently, there’s no other choice. But like I said above, I put the enhancement into trac, and hopefully, you won’t have to hack the core in the next version of WordPress, as the change will already be there.

    Otto42: thanks for the tip and for submitting the change to trac. It’s impressive how quickly changes can get added to the system.

    whooami: I’ll file the shuffle() tip away in case I want to do something like that. I appreciate your addition.

    Thanks, Otto42. This is exactly the behaviour that I need. Hope it makes it into 2.3 so I don’t need to hack the core every time 😐

    Otto42: This was the greatest help I have ever received. I needed a random post generator/plugin that would only call on a single category, without creating a hundred template files ( as I’ve seen with some banner rotation plugins ) that allowed for the use of an image with javascript rotation, the_excerpt_reloaded plugin, Post Templates by Category plugin, and Custom Write Panel plugin not to mention this site has five Loops and CSS was necessary to maintain the look.

    I want to throw in that it’s still possible to add &showposts=1 or however many posts you would like. Again I only hope WordPress adds this in and thanks again for the brilliant assistance.

    Thanks whooami 🙂

    Thanks, Otto. First rate.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Add random sort option to query_posts’ is closed to new replies.