Ordering by multiple meta keys wordpress

Im aware there are several posts relating to this issue but i am yet to find a fix for my current problem.

Im reasonably new to WordPress and am still getting used to it, however…

Read More

The Problem

I’m implementing a front end sorting system for users to filter out schools, i am using a rating plugin (comment rating field pro) and the issue i have is that when users sort by star rating i also want it to sub sort by number of reviews (comments). Currently it is only sorting by rating and simply ignoring the number of comments and sorting by title. I have tried various combinations of query parameter arrays such as meta_query and multiple meta keys but the issue appears to be that it refuses to sort the second value. Both are numbers in the database.

TL:DR it needs to sort by stars and then by number of reviews.

Unfortunatly a custom query is out of the question as the array is being fed into another plugin which appears to only accept an array of args.

This is the sort of idea im going for (this does not work)

Array ( [post_type] => surf-school [posts_per_page] => 12 [paged] => 0 [meta_key] =>      crfp-average-rating [orderby] => meta_value crfp-total-rating [order] => DESC )

Any help would be greatly appreciated as its driving me insane!

Thanks in advance.

Update

I have managed to fix this using from a comment reply to this post which now appears to have been deleted 🙁

I managed it by adding a filter to wp_query and forcing a custom order by as seen here Plugin API/Filter Reference/posts orderby – Codex

function edit_posts_orderby($orderby_statement) {
$orderby_statement = "wp_postmeta.meta_value DESC, wp_posts.comment_count DESC";
return $orderby_statement;

}

placed in functions.php and

add_filter('posts_orderby', 'edit_posts_orderby');

placed just before the query. Hope this helps someone else 🙂

Related posts

Leave a Reply