Title: Random order
Last modified: May 8, 2018

---

# Random order

 *  Resolved [ryochan819](https://wordpress.org/support/users/ryochan819/)
 * (@ryochan819)
 * [8 years ago](https://wordpress.org/support/topic/random-order-24/)
 * Hi,
 * May I know if search result in random order is only allowed in premium version?
 * I see relevant and post date option but I dont see where I can change the order
   query.
 * Do I need to look at php file?

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [8 years ago](https://wordpress.org/support/topic/random-order-24/#post-10260714)
 * There’s no option for it in Premium, either. It’s as simple as appending `&orderby
   =rand` to your search query, or using any other way to set the `orderby` parameter
   to `rand`.
 * However, there’s a bug in the current version of Relevanssi which actually makes
   the random ordering not work. It will be fixed in the next version of Relevanssi,
   but if you want a quick fix, modify the /lib/common.php file in Relevanssi to
   change this line:
 * `if ( ! isset( $data[0]->$primary_key ) ) {`
 * to
 * `if ( 'rand' !== $primary_key && ! isset( $data[0]->$primary_key ) ) {`
 * That will make the random sorting possible.
 *  Thread Starter [ryochan819](https://wordpress.org/support/users/ryochan819/)
 * (@ryochan819)
 * [8 years ago](https://wordpress.org/support/topic/random-order-24/#post-10260841)
 * Hi,
 * How do I add rand to search query? I see something like
    $default_order = get_option(‘
   relevanssi_default_orderby’, ‘relevance’ ); in search.php but I don’t know where
   to add it
 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [8 years ago](https://wordpress.org/support/topic/random-order-24/#post-10261028)
 * If you want to make all searches random, just add this to your theme functions.
   php:
 *     ```
       add_filter( 'relevanssi_orderby', 'rlv_random_order' );
       function rlv_random_order( $orderby ) {
           return 'rand';
       }
       ```
   
 * That should do the trick (but like I said above, won’t work in the current version
   of Relevanssi, unless you add the fix outlined above).
 *  Thread Starter [ryochan819](https://wordpress.org/support/users/ryochan819/)
 * (@ryochan819)
 * [8 years ago](https://wordpress.org/support/topic/random-order-24/#post-10265596)
 * Thanks for the help. I found that the random result keep changing every time 
   I go to second page or other page of search result. Is the anyway to keep 1 random
   order in 1 search result?
 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [8 years ago](https://wordpress.org/support/topic/random-order-24/#post-10268661)
 * That’s why they’re called random results. There’s no state stored between page
   loads – Relevanssi doesn’t know that loading the page 2 is from the same session
   than the page 1.
 * You can specify the seed value for the random number generator. If you use
 * `return 'rand(4)';`
 * in the function, then you’ll get random results that are stable – but of course
   the results are then the same random set every time and for every user.
 * What you can do is to use date and time for the source of the random seed. If
   you use `intval(date("Ymdh"))` as the seed, you get random results that change
   every hour. Within that hour they’re stable, and page two will come from the 
   same set from page one.
 * That’s one fairly easy approach. Another way would be to carry along some identifying
   variable that tells you which page loads come from the same search.
 *  Thread Starter [ryochan819](https://wordpress.org/support/users/ryochan819/)
 * (@ryochan819)
 * [8 years ago](https://wordpress.org/support/topic/random-order-24/#post-10271065)
 * That sounds good. Can you teach me where to put it?
 * Can the source goes like intval(search) so that it change source everytime you
   click search? like refresh the source when you start searching
    -  This reply was modified 8 years ago by [ryochan819](https://wordpress.org/support/users/ryochan819/).
    -  This reply was modified 8 years ago by [ryochan819](https://wordpress.org/support/users/ryochan819/).
 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [7 years, 12 months ago](https://wordpress.org/support/topic/random-order-24/#post-10272359)
 * Actually, you can try this:
 *     ```
       add_filter( 'relevanssi_orderby', 'rlv_random_order' );
       function rlv_random_order( $orderby ) {
           $seed = intval( date("md") . str_replace( '.', '', $_SERVER['REMOTE_ADDR'] ) );
           return "rand($seed)";
       }
       ```
   
 * This uses the user IP address and a combination of month and day numbers for 
   the seed. That’ll give each user a different set of random results every day.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Random order’ is closed to new replies.

 * ![](https://ps.w.org/relevanssi/assets/icon-256x256.png?rev=2025044)
 * [Relevanssi - A Better Search](https://wordpress.org/plugins/relevanssi/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/relevanssi/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/relevanssi/)
 * [Active Topics](https://wordpress.org/support/plugin/relevanssi/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/relevanssi/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/relevanssi/reviews/)

 * 7 replies
 * 2 participants
 * Last reply from: [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * Last activity: [7 years, 12 months ago](https://wordpress.org/support/topic/random-order-24/#post-10272359)
 * Status: resolved