Last active
October 3, 2020 01:54
-
-
Save jchristopher/bc557b4b1dda1aad31606d4f3e744869 to your computer and use it in GitHub Desktop.
Ranomize SearchWP 4 results (with pagination)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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