Using pre_get_posts hook
-
Hi,
I was wondering if pre_get_posts was supported, i.e for ordering results in a WP loop by descending rates (from best ratings to worst) for category pages (archive.php):
function order_by_rating_126($query) { if ( $query->is_main_query() && !is_admin() ) { if ($query->is_category()){ //for all entries... $query->set('posts_per_page', -1); $test123 = array( 'spr_votes' => 'DESC', 'spr_rating' => 'DESC', ); $query->set( 'orderby', $test123); // $query->set( 'orderby', 'spr_votes spr_rating'); // $query->set( 'orderby', 'spr_rating spr_votes'); // sorting combination attributes won't work... in first instance } } } add_action('pre_get_posts', 'order_by_rating_126');
I used spr_votes but suppose is not logically ok, because I mean “mean ratings” not numbers of vote. Any help would be appreciated… thanks in advance!
- The topic ‘Using pre_get_posts hook’ is closed to new replies.