How to order by multiple meta keys?
custom-post-type, meta-key, sorting, wordpress
Solution
This is something that WordPress added support for in 4.2: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/
Please note: It is not so obvious, but for ordering with multiple meta keys you have to give each meta_query a name and then use these names in the orderby.
In your case you'll probably want to do something like this:
$args = array(
'post_type' => 'event',
'meta_query' => array(
'relation' => 'AND',
'event_start_date_clause' => array(
'key' => '_event_start_date',
'compare' => 'EXISTS',
),
'event_start_time_clause' => array(
'key' => '_event_start_time',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'event_start_date_clause' => 'ASC',
'event_start_time_clause' => 'ASC',
),
);
$loop = new WP_Query( $args );
Problem
I’m using a custom loop to display my events on a page, I get it fine ordering by the event start date using the below: ``` $args = array( 'post_type' => 'event', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => '_event_start_date'); $loop = new WP_Query( $args ); ``` But the meta_key option only allows one value. How to use two values (`_event_start_date` and `_event_start_time`)?