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