admin管理员组

文章数量:1289543

I'm working on a site that has blog posts with performers' info under a single category that can be filtered using a custom taxonomy (both parent and child terms). To keep it as fair as possible, the site owners would like the category and filter results display to be in random order. This is what I was able to find to 'randomize':

add_action( 'pre_get_posts', 'my_change_sort_order'); 
function my_change_sort_order($query){
    if(is_category('artstour-roster') OR is_tax('artistic_disciplines') OR is_tax('geographic_availability') OR is_tax('additional_services') OR is_tax('core_audiences') OR is_tax('additional_populations')):
       //Set the order ASC or DESC
       $query->set( 'order', 'ASC' );
       //Set the orderby
       $query->set( 'orderby', 'rand' );
    endif;    
};

This displays the post excerpts in random order with 6 per page. However, if you have more than one page of results and move between pages, the number of results stays the same, but moving to the next or previous page gives a new random order, which causes repeats and omissions.

For example, a filter of the 'A cappella' child taxonomy term produces 7 results. When moving to the second page of results, only one result entry is displayed, but that single result is randomized again, showing one of the results previously seen on the prior page and thereby leaving one of the filtered excerpts omitted from the filter results entirely.

Is there a way to 'lock in' the randomized result set across pages?

I'm working on a site that has blog posts with performers' info under a single category that can be filtered using a custom taxonomy (both parent and child terms). To keep it as fair as possible, the site owners would like the category and filter results display to be in random order. This is what I was able to find to 'randomize':

add_action( 'pre_get_posts', 'my_change_sort_order'); 
function my_change_sort_order($query){
    if(is_category('artstour-roster') OR is_tax('artistic_disciplines') OR is_tax('geographic_availability') OR is_tax('additional_services') OR is_tax('core_audiences') OR is_tax('additional_populations')):
       //Set the order ASC or DESC
       $query->set( 'order', 'ASC' );
       //Set the orderby
       $query->set( 'orderby', 'rand' );
    endif;    
};

This displays the post excerpts in random order with 6 per page. However, if you have more than one page of results and move between pages, the number of results stays the same, but moving to the next or previous page gives a new random order, which causes repeats and omissions.

For example, a filter of the 'A cappella' child taxonomy term produces 7 results. When moving to the second page of results, only one result entry is displayed, but that single result is randomized again, showing one of the results previously seen on the prior page and thereby leaving one of the filtered excerpts omitted from the filter results entirely.

Is there a way to 'lock in' the randomized result set across pages?

Share Improve this question asked May 17, 2021 at 15:42 Jim GoodrichJim Goodrich 11 bronze badge 2
  • 1 Check an answer from this similar question - wordpress.stackexchange/questions/31647/… – anton Commented May 17, 2021 at 18:27
  • 1 This solution worked perfectly, I just didn't know what to search for. Thank you for the help. – Jim Goodrich Commented May 18, 2021 at 17:12
Add a comment  | 

1 Answer 1

Reset to default 0

I think you can't with orderby and rand. Instead you could create a custom table where you generate random integer values and connect them to the posts table. Then use posts_join filter to join your custom table to the posts query and then use posts_orderby filter to order by your joined random field. Now your random list should be paginatable.

本文标签: custom taxonomyKeeping session instance of random display results over pagination breaks