Skip to main contentSkip to footer

Do you want to order your search results in random order? Here’s how:

add_filter( 'relevanssi_hits_filter', 'shuffle_search' );
function shuffle_search( $hits ) {
    shuffle( $hits[0] );
    return $hits;
}

Add this function to your site and the search results will appear in random order. You can also do this:

add_filter( 'relevanssi_modify_wp_query', function ( $query ) {
  $query->set( 'orderby', 'rand' );
  return $query;
}

However, both of these solutions only work if all the search results are on the same page. If the results span multiple pages, they will be shuffled between page switches, and the second page can have results from the first page on it, and some posts may never appear in results.

Here’s a more complicated solution that solves the issue by setting the random number generator seed every hour. Searches within that hour will return the results in the same order. This means pagination will work (unless the searcher loads the first page on 00:59 and the second page on 01:00, in which case the results come from a different set).

add_filter( 'relevanssi_hits_filter', 'shuffle_search' );
function shuffle_search( $hits ) {
    $date = date( 'Ymdh' );
    seeded_shuffle( $hits[0], $date );
    return $hits;
}

function seeded_shuffle( &$items, $string ) {
    mt_srand( strlen( $string ) ); 
    for ( $i = count( $items ) - 1; $i > 0; $i-- ) { 
        $j           = @mt_rand( 0, $i ); 
        $tmp         = $items[ $i ]; 
        $items[ $i ] = $items[ $j ]; 
        $items[ $j ] = $tmp; 
    } 
}

The seeded shuffle function is from PHP documentation.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Indexing and searching PDFs in WordPress

…attachment indexing is a two-step process. First, the attachment content is read and stored in a custom field (_relevanssi_pdf_content). This step alone does not index the attachment content – it just makes it available for future indexing and ensures you don’t have to read the attachment contents many times. The second……step is the actual indexing. Here Relevanssi offers two different methods. You can choose to index the attachment post type, in which case the search results will include the attachment posts. The other method is to index the attachment content for the parent post of the attachment, in which case…Relevanssi Premium users have asked for PDF indexing since day one, and version 2.0 finally introduced this feature. Coming up with a fast and reliable method hasn’t been easy, but we’re pretty proud of what we have now. Our PDF indexer doesn’t tax your server as it runs as a…

User Access Manager
Relevanssi has a conflict with User Access Manager plugin. Both plugins attach to the same `the_posts` filter hook with the…
SearchWP Live Ajax Search

Relevanssi doesn’t ship with a live search or autocomplete feature that would display results as the user types the search terms. There are many other plugins that provide this functionality, but few of these plugins work with Relevanssi. SearchWP Live Ajax Search is the best one that does. It’s very…

Related Posts:

Comment Section:

1 Comment. Leave new

  • How do I make this work for multiple search result pages. Currently each page reshuffles making so results show up twice and some not at all.
    I’ve seen this code that should work but I’m not sure how to include it in the function to make it work:

    if( !is_admin() ) {

    $seed = $_SESSION[‘seed’];

    if (empty($seed)) {

    $seed = rand();

    $_SESSION[‘seed’] = $seed;

    }

    $orderby_statement = ‘RAND(‘.$seed.’)’;

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed