I am trying to sort posts based on rating (using wp-postrating plugin) and numbers of comments. I have made this using wpdb, but i kinda wish to make it easier and use it with wp_query. Seems that I can not find any way to achieve this because I do not know any method to orderby two items in $args.

My wpdb code looks like this...

$wpdb->get_results("SELECT wp_postmeta.meta_value, id, post_title, comment_count FROM wp_posts INNER JOIN wp_postmeta ON wp_postmeta.post_id = wp_posts.id WHERE post_type='post' AND wp_postmeta.meta_key='ratings_average' ORDER BY wp_postmeta.meta_value DESC, comment_count DESC");

Any idea?

2 Answers 2


You can do it with wp_query

$args = array(  'post_type'     => 'post',
                'numberposts'   => -1,
                'posts_per_page'=> -1,
                'orderby'       => 'meta_value_num',
                'meta_key'      => 'ratings_average',
                'order'         => 'DESC',
$posts = new WP_Query($args);

Now the result is DESC Order.

  • Yes, but I also need to sort it DESC by comments number. Jun 22, 2013 at 7:38
  • I'm pretty sure this answer doesn't solve Iulians problem. You just showed how to order posts by meta_value. Am I missing something? Jun 22, 2013 at 7:45
  • 1
    sorry i didnt read the full problem
    – ksr89
    Jun 22, 2013 at 7:49

First of all, you can use multiple fields in orderby argument. Codex shows you how to do it:

$query = new WP_Query( array( 'post_type' => 'page', 'orderby' => 'title menu_order', 'order' => 'ASC' ) );

And it should solve your problem. (It should, but if you want to sort DESC it probably won't, because of this WordPress bug: http://core.trac.wordpress.org/ticket/17065)

Of course you always can use posts_orderby filter to create your own ORDER BY part of query.

function my_posts_orderby($orderby_statement) {
    $orderby_statement = "YOUR ORDER BY STATEMENT";
    return $orderby_statement;
add_filter('posts_orderby', 'my_posts_orderby');

Just add this filter just before you call your query and remove it after.

  • If I use this: $args = array( 'posts_per_page' => 5, 'paged' => $paged, 'meta_key' => 'ratings_average', 'orderby' => 'meta_value comment_count', 'order' => 'DESC' ); it works somehow. But it`s not filtering right. Jun 22, 2013 at 7:46
  • What do you mean by "not filtering right"? You can always look at SQL query that was generated by wp_query (just var_dump wp_query object). Jun 22, 2013 at 7:53
  • For example I have a post with 5 average rating, 0 comments. I have one with 4 average rating and 3 comments, and I have another one with 5 average and 2 comments. By using that exact code the order is just as I wrote it...should be 1 3 2. Jun 22, 2013 at 7:58
  • What SQL does this wp_query generate? Jun 22, 2013 at 8:02
  • SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') AND (wp_postmeta.meta_key = 'ratings_average' ) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value,wp_posts.comment_count DESC LIMIT 0, 5) Jun 22, 2013 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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