Sorting custom taxonomy by rating
-
I have a number of custom taxonomies on my website. I would like the taxonomy pages to sort the results in descending order of their rating.
The following WordPress documentation page shows an example of exactly what I’m trying to accomplish on the taxonomy pages. And I mean EXACTLY. Supposedly their example code works “In tandem with a rating plugin that uses a separate database table, sorting by rating value can be achieved with posts_orderby in tandem with the post_join_paged filter.”
https://codex.ww.wp.xz.cn/Plugin_API/Filter_Reference/posts_orderby
The following query works perfectly in mySQL:
SELECT * FROM wp_posts LEFT JOIN wp_spr_rating ON wp_posts.ID = wp_spr_rating.post_id
ORDER BY (wp_spr_rating.points / wp_spr_rating.votes) DESCBut when I try to implement it as follows (exactly the same as the example, with only the table and field names changed), the $post_count is 0.
function edit_posts_join_paged($join_paged_statement) {
$join_paged_statement = “LEFT JOIN wp_spr_rating ON wp_posts.ID = wp_spr_rating.post_id”;
return $join_paged_statement;
}function edit_posts_orderby($orderby_statement) {
$orderby_statement = “(wp_spr_rating.points / wp_spr_rating.votes) DESC”;
return $orderby_statement;
}add_filter(‘posts_orderby’, ‘edit_posts_orderby’);
add_filter(‘posts_join_paged’,’edit_posts_join_paged’);
$custom_query = new WP_Query( $args );
remove_filter(‘posts_join_paged’,’edit_posts_join_paged’);
remove_filter(‘posts_orderby’, ‘edit_posts_orderby’);If I leave out the add_filter calls, everything works as expected, except that the results are sorted in descending order by publication date. If I include the add_filter calls, the result is empty.
Any thoughts on why this might be or if I’m doing something wrong?
The topic ‘Sorting custom taxonomy by rating’ is closed to new replies.