Sorry, there’s no simple code snippet for this, it would require a custom implementation. If customizations beyond the available features are required for your site, please consider hiring someone for custom work https://fvrr.co/32e7LvY
Found the solution, trying now to do the same with the search results, will update if succeeded.
// Initialize session if not already started
if (!session_id()) {
session_start();
}
add_filter(
'posts_orderby',
function($orderby, $query){
if ( $query->get( 'post_type' ) === 'hp_listing' && is_post_type_archive('hp_listing') && empty($_GET['_sort']) ) {
if (isset($_SESSION['listing_seed'])) {
$seed = $_SESSION['listing_seed'];
} else {
$seed = time(); // Use the current timestamp as the seed
$_SESSION['listing_seed'] = $seed;
}
$orderby = "RAND($seed)";
}
return $orderby;
},
10,
2
);
Just a short explanation about the code: it will randomize the order for the session, this way it will not show the same listings on other pages and the next time visiting the site it will randomize again.
Here’s the code for randomizing the search results, can someone please have a look at this code and see why it’s not keeping consistent results over pagination? (i.e. it will show some results from the previous page)
// Initialize session if not already started
if (!session_id()) {
session_start();
}
add_filter( 'pre_get_posts', 'randomize_search_results' );
function randomize_search_results( $query ) {
if ( $query->is_search() && $query->get( 'post_type' ) === 'hp_listing' ) {
if ( empty( $_GET['_sort'] ) ) {
if (isset($_SESSION['search_seed'])) {
$seed = $_SESSION['search_seed'];
} else {
$seed = time(); // Use the current timestamp as the seed
$_SESSION['search_seed'] = $seed;
}
$query->set( 'orderby', 'rand' );
$query->set( 'seed', $seed );
}
}
return $query;
}