Random Ordering in FacetWP

To sort results randomly within a FacetWP Template, you can set orderby to rand:

return array(
  'post_type' => 'post',
  'post_status' => 'publish',
  'orderby' => 'rand',

Since FacetWP is ajax-based, the problem is that the results will be re-randomized on each facet interaction, which is less than desirable.

Fortunately, we can fix this issue with some extra code in functions.php:

function preserve_random_order( $orderby ) {
    $seed = floor( time() / 10800 ); // randomize every 3 hours
    $orderby = str_replace( 'RAND()', "RAND({$seed})", $orderby );
    return $orderby;
add_filter( 'posts_orderby', 'preserve_random_order' );

Hat tip to Alex Durston at Amp Web Design for the code example.