Randomize listing order code still dysfunctional

This code randomizes the listings if you go to all categories from the homepage, but if you click on an individual category from the homepage, it doesn’t perform the function.

add_filter( 
 	'posts_orderby', 
 	function($orderby, $query){
  		if ( $query->get( 'post_type' ) === 'hp_listing' && is_post_type_archive('hp_listing') && empty($_GET['_sort']) ) {
   			$orderby = 'RAND()';
  		}

  		return $orderby;
 	}, 
 	10, 
 	2 
);

Is there an alternative that would solve this problem? Thank you!

Please try this PHP code snippet. It works for both all listing categories and specific listing category pages.

add_filter( 
 	'posts_orderby', 
 	function($orderby, $query){
  		if ( ($query->get( 'post_type' ) === 'hp_listing' && is_post_type_archive('hp_listing') && empty($_GET['_sort'])) || is_tax('hp_listing_category') ) {
   			$orderby = 'RAND()';
  		}

  		return $orderby;
 	}, 
 	1000, 
 	2 
);

Thank you so much! This works perfectly.

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