Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active October 3, 2020 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/bc557b4b1dda1aad31606d4f3e744869 to your computer and use it in GitHub Desktop.
Save jchristopher/bc557b4b1dda1aad31606d4f3e744869 to your computer and use it in GitHub Desktop.
Ranomize SearchWP 4 results (with pagination)
<?php
// In order to paginate randomized results, we need a seed.
add_action( 'init', function() {
if ( ! isset( $_COOKIE['seed'] ) ) {
setcookie( 'seed', rand(), 0, COOKIEPATH, COOKIE_DOMAIN );
}
} );
// Tell SearchWP to randomize the results based on the stored seed.
add_filter( 'searchwp\query\mods', function( $mods ) {
$seed = isset( $_COOKIE['seed'] ) ?: 0;
$mod = new \SearchWP\Mod();
$mod->order_by( "RAND($seed)", '', 1 );
$mods[] = $mod;
return $mods;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment