Random order
-
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?
-
There’s no option for it in Premium, either. It’s as simple as appending
&orderby=randto your search query, or using any other way to set theorderbyparameter torand.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.
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 itIf 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).
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?
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.
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.
-
This reply was modified 8 years ago by
ryochan819.
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.
-
This reply was modified 8 years ago by
The topic ‘Random order’ is closed to new replies.