I am using the pre_get_posts
hook to alter a query before it runs.
The posts in question are performances and so are date and time based.
I want to alter the query to get all performances which are in the future (i.e. the date is greater than today or the date is today and the time is greater than or equal to the current time). This works but when I try to order them primarily by date ascending and secondarily by time ascending the order is not as expected.
The posts are ordered by date correctly but only today's performances are ordered correctly by time with the times of performances on other days being ordered randomly.
I believe this has something to do with the naming of the meta_query arrays in relation to the orderby parameters but cannot work out how to solve this.
My code is as follows:
//No limit on number to get
$query->set('posts_per_page', -1);
//Get future performances
$query->set('meta_query', array(
'relation' => 'AND',
array(
'relation' => 'OR',
'performance_date' => array(
'key' => 'performance_date',
'value' => date('Ymd'),
'compare' => '>',
'type' => 'NUMERIC'
),
array(
'relation' => 'AND',
'performance_date' => array(
'key' => 'performance_date',
'value' => date('Ymd'),
'compare' => '=',
'type' => 'NUMERIC'
),
'performance_time' => array(
'key' => 'performance_time',
'value' => date('H:i'),
'compare' => '>=',
'type' => 'TIME'
)
)
)
));
//Order by closest performance first
$query->set('orderby', array(
'performance_date' => 'ASC',
'performance_time' => 'ASC'
));