0

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?

2

1 Answer 1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.