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.

How can I show random listings on properties page? Because right now it shows the latest listing to oldest listing

Hi,

Please check this topic.

I have seen many solutions to this in forum but none works now.

Is there a workaround to make this?

thanks

Hi,

Please check the solution in this topic. Also, please note that you need to add this code using this documentation How to add custom code snippets - HivePress Help Center.

I hope it helps