How to query_posts using meta_query to orderby meta_key AND have a secondary sort by date?

Hopefully you’ve figured this out by now, but if you haven’t you should be able to use the “posts_orderby” filter to dial in a specific order for your query. I’m not going to give a full solution here, but you can refer to this post for more: http://mitcho.com/blog/how-to/external-orders-in-wordpress-queries/

EDIT:

Here’s the Documentation – basically you can just override the ORDER BY clause of the SQL: http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_orderby

And here’s the example code from the docs:

add_filter('posts_orderby', 'edit_posts_orderby');
add_filter('posts_join_paged','edit_posts_join_paged');

function edit_posts_join_paged($join_paged_statement) {
$join_paged_statement = "LEFT JOIN wp_gdsr_data_article gdsra ON gdsra.post_id = wp_posts.ID";
return $join_paged_statement;   
}

function edit_posts_orderby($orderby_statement) {
$orderby_statement = "(gdsra.user_votes_total_sum/gdsra.user_votes_count) DESC";
return $orderby_statement;
}

Leave a Comment