You can do this by naming index of meta query, then passing array of these names in orderby
parameter
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '10',
'meta_query' => array(
'relation' => 'OR',
'with_time' => array(
'key' => 'TIME_meta_key',
'compare' => 'EXISTS'
),
'without_time' => array(
'key' => 'TIME_meta_key',
'compare' => 'NOT EXISTS',
/* 'value' => 'xxx' The bug has been fixed ; */
),
),
/* First order posts those does not have meta key then those have meta key in ascending order */
'orderby' => array(
'without_time' => 'ASC',
'with_time' => 'ASC'
),
);
In orderby array first key is without_time
then with_time
thus it will bring up all the posts first those does not have TIME_meta_key
however, these posts will not have order (Will be default based on ID) because meta key does not exist. Then it will join the posts those have meta key TIME_meta_key
in ascending order.
So the final outcome will be posts without meta key orderby ID
then posts with meta key order by value ascending
.
NOTE 1: these syntax are only supported since WordPress version 4.2
NOTE 2: Another bug related toNOT EXIST
has been fixed in WordPress
3.9