您可以通过命名元查询的索引,然后将这些名称的数组传入orderby
参数
$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\'
),
);
在orderby数组中,第一个键是
without_time
然后
with_time
因此,它将首先提出所有没有的职位
TIME_meta_key
然而,这些帖子将没有顺序(将根据ID默认),因为元键不存在。然后它将加入那些有元密钥的帖子
TIME_meta_key
按升序排列。
因此,最终结果将是posts without meta key orderby ID
然后posts with meta key order by value ascending
.
注1:这些语法仅在WordPress 4.2版之后才受支持。注2:另一个与NOT EXIST
已在WordPress 3.9中修复