I’m working on a site that lists events. Right now this query lists events by date:
$args = array(
'post_type' => 'espresso_event',
'post_status' => 'publish',
'posts_per_page' => 8,
'category_name' => 'homepage',
'meta_key' => 'event_start_date',
'meta_query' => array(
array(
'key' => 'event_start_date',
'value' => date('Y-m-d'),
'compare' => '>=', // compares the event_start_date against today's date so we only display events that haven't happened yet
'type' => 'DATE'
)
),
'orderby' => 'meta_value',
'order' => 'ASC'
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query($args);
However, some events take place on the same date at different times and I need it to order posts first by date and then by a custom meta called event_start_time. Problem is, I’m at a loss on how to do that. wp_query doesn’t seem capable of ordering by two custom meta.
Is this something I can even do?
In pure
SQL
you can use comma separated strings to do what you want– that is, something likeORDER BY post_type,post_name,post_date
. However,WP_QUERY
will not allow that kind of thing to pass through. There is a rigid set of allowed values.There are a couple of ways I can think of to do this.
You can “push” query vars to
WP_Query
just by adding them to your$args
. Caveat: I do not know if this is intended behavior. It may be a quirk in the code base.Add a filter on
posts_orderby
…… and you can order to your heart’s content.
The (probably) more canonical way to do it is to check the query vars, or other information, and alter the query accordingly.
The drawback of the second method is that you have to create a filter for each query that you need to alter.