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
);
yevhen:
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.
system
Closed
July 7, 2023, 1:44am
5
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
pepeg
May 26, 2025, 7:13pm
8
I have seen many solutions to this in forum but none works now.
Is there a workaround to make this?
thanks
andrii
May 27, 2025, 9:11am
10
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