How to sort multiple wordpress custom field values? How to sort multiple wordpress custom field values? wordpress wordpress

How to sort multiple wordpress custom field values?


Thanks to Bainternet I found the solution:

function orderbyreplace($orderby) {    return str_replace('menu_order', 'mt1.meta_value, mt2.meta_value', $orderby);}

and...

$args = array(  'post_type'=>'Events',  'orderby' => 'menu_order',  'order' => 'ASC',  'meta_query' => array(        array(            'key' => 'Start_Hour',            'value' => '',            'compare' => 'LIKE'        ),        array(            'key' => 'Start_Minute',            'value' => '',            'compare' => 'LIKE'        )    ));add_filter('posts_orderby','orderbyreplace');$loop = new WP_Query( $args );remove_filter('posts_orderby','orderbyreplace');


I think this changed slightly in Wordpress 3.7.

I had to change

return str_replace('menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);

into

return str_replace('wp_posts.menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);

Thanks for the initial solution! [Edited]


Here's an example of using more than one meta_key and orderby that I believe should work:

$params = array('post_type' => 'product','meta_query' => array(    array(            'key' => 'price',            'value' => '',            'compare' => 'LIKE'    ),    array(            'key' => 'size',            'value' => '',            'compare' => 'LIKE'    )),'orderby' => 'price size','order' => 'ASC');$query = new WP_Query;$resulting_obj = $query->query($params);

You'll need to play with the meta_query items a bit more, especially the 'value' parameter. Please have a good look at http://codex.wordpress.org/Class_Reference/WP_Query in the 'Custom Field Parameters' section.