Change order of listings to random

Change order of listings to random

This topic was treated in the past but the solution provided at that time is no longer current because I tried to use the solution offered by yevhen but there seems to be a conflict inside the listing (Please see the print screen). The listings are random displayed in page, but inside the listing is an issue.

Is there any updated solution for this issue?

Post from the past:

Solution provided in the past by yevhen:

add_action(
	'pre_get_posts',
	function($query){
		if(!is_admin() && 'hp_listing' === $query->get('post_type') && 'listings_view_page' === hivepress()->router->get_current_route_name()) {
			$query->set('orderby', 'rand');
		}
	},
	1000
);

Thank you in advance!

Please try this PHP snippet instead but please note that it can require further customization. If you are not familiar with the code customization then please consider hiring someone for custom work https://fvrr.co/32e7LvY

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

Hi yevhen,
Thank you for your support. It works.
Best regards,

Hi yevhen,

It seems that the code provided by you displays random listings at each page refresh only when I’m logged in. If I am not logged in, the listings are displayed in the same order every time the page is refreshed.

Thank you!

This issue may occur if your website is cached, maybe there’s a caching plugin? Then the page is server as a static HTML (so PHP code doesn’t run on every page refresh) and its content is the same.

Hi ihor,
I tested without cache plugin and it works. But with the cache function disabled from LiteSpeed, the site is loading slowly :pensive:
Thank you!

Yes, unfortunately it’s not possible to combine random order with cache since the caching purpose is to avoid running PHP scripts on every page refresh.

Yep… Thank you ihor for the information. I will try to see if the provider of the hosting can make some improvements on the server regarding the site’s loading speed.

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