Randomize listings order and pages

Hello,

Is there a way to exclude listings already displayed if random order is used? reference: Change order of listings to random

The issue is when going to page 2, it is showing listings from page 1.
we would like to randomize the order but not to show the same listing twice.

Hi,

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.

Hi all,

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;
}

Thank you for waiting. Please try this PHP code snippet. But please note that it can require further advanced customization and testing.

add_filter(
	'posts_orderby', 
	function($orderby, $query) {
		if('hp_featured' === $query->get('meta_key') || !$query->is_search() || !$query->get( 'post_type' ) === 'hp_listing'){
			return $orderby;
		}

   	 	$seed = null;
    	
		if (empty($_SESSION['seed'])) {
      		$seed = rand();
      		$_SESSION['seed'] = $seed;
    	}else{
			$seed = $_SESSION['seed'];
		}

    	$orderby = 'RAND('.$seed.')';
    	return $orderby;
	},
	1000,
	2
);
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.