-
-
Save eugenehp/9cf634f4463664bf8a6e to your computer and use it in GitHub Desktop.
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 | |
session_start(); | |
add_filter( 'posts_orderby', 'randomise_with_pagination' ); | |
function randomise_with_pagination( $orderby ) { | |
if( is_front_page() ) { | |
// Reset seed on load of initial archive page | |
if( ! get_query_var( 'paged' ) || get_query_var( 'paged' ) == 0 || get_query_var( 'paged' ) == 1 ) { | |
if( isset( $_SESSION['seed'] ) ) { | |
unset( $_SESSION['seed'] ); | |
} | |
} | |
// Get seed from session variable if it exists | |
$seed = false; | |
if( isset( $_SESSION['seed'] ) ) { | |
$seed = $_SESSION['seed']; | |
} | |
// Set new seed if none exists | |
if ( ! $seed ) { | |
$seed = rand(); | |
$_SESSION['seed'] = $seed; | |
} | |
// Update ORDER BY clause to use seed | |
$orderby = 'RAND(' . $seed . ')'; | |
} | |
return $orderby; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment