Randomise results from a category page?

I have my standard category page. On this page, I’d like to put a button which, when clicked, randomises the results shown on the category page.

I know I could make a rand-category.php page and send the user on to that page. Ideally though, I’d like a more elegant solution, possibly something that refreshes the results without moving the user on/away from the page.

Related posts

Leave a Reply

2 comments

  1. You can actually change any of the query parameters (like the order) by adding them to the url.

    For instance, the sample url below would randomize the posts on the category archive every time you refreshed, randomizing the results.

    http://sample.com/category/uncategorized/?orderby=rand
    

    Below is a basic form with the orderby value that would refresh whatever page/template it shows on. (you would just need to add it to archive.php in your theme, or to the sidebar via an HTML widget)

    <form method="get">
    <input type="hidden" name="orderby" value="rand" />
    <input type="submit" value="Randomize" />
    </form>
    

    You would just need to work out the ajax to refresh the HTML element holding your loop. (the guy over at this post is doing a ajax comment refresher, you could look at how he is reloading his loop via ajax

  2. Simple solution I guess :

    make a button/link with url to same page locationn
    (ex for page “Our Page”: Random results )

    modify the query args to randomise the results

    $query = new WP_Query( array ( 'orderby' => 'rand') );