How to sort posts by highest rated
-
First off I’d like to commend this plugin. It’s really the best I’ve used on my site
I’d like to sort my posts by highest rated on the front page. I saw that someone already raised an issue like this on the group and a code was provided. I tried it and it worked but there’s still a modification I’m in need of.
For instance two posts have been each rated 5 stars and one of the posts has a high number of people voting than the other, I’d like the post with the higher visitor votes to precede the other.Below is the code that worked for me
`add_filter(‘posts_join_paged’,’yasr_join_paged’);
add_filter(‘posts_where’, ‘yasr_where’);
add_filter(‘posts_groupby’, ‘yasr_groupby’);
add_filter(‘posts_orderby’, ‘yasr_posts_orderby’);function yasr_join_paged($join_paged_statement) {
if(is_home() && is_main_query()) {
global $wpdb;
return “, {$wpdb->prefix}yasr_log AS YL”;
}
return $join_paged_statement;
}function yasr_where($where) {
if(is_home() && is_main_query()) {
global $wpdb;
return ” AND YL.post_id = {$wpdb->prefix}posts.ID”;
}
return $where;
}function yasr_groupby($groupby) {
if(is_home() && is_main_query()) {
global $wpdb;
return ” YL.post_id, {$wpdb->prefix}posts.ID”;
}
return $groupby;
}function yasr_posts_orderby($orderby_statement) {
if(is_home() && is_main_query()) {
return ” (ROUND(SUM(YL.vote) / COUNT(YL.post_id), 1)) DESC”;
}
return $orderby_statement;}Thanks
- The topic ‘How to sort posts by highest rated’ is closed to new replies.