您可以使用meta_query
它获取键存在或不存在的帖子,然后对元键进行排序,并对具有相同值或没有键的帖子进行二级排序:
$args = array(
\'post_type\' => \'my_product\'
\'tax_query\' => array(
array(
\'taxonomy\' => \'my_product_cat\',
\'field\' => \'slug\',
\'terms\' => array( $category->slug )
)
),
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'vip_row\',
\'compare\' => \'NOT EXISTS\',
),
array(
\'key\' => \'vip_row\'
\'compare\' => \'EXISTS\',
)
),
\'orderby\' => array(
\'meta_value_num\' => \'DESC\',
\'date\' => \'ASC\',
)
);
$query = new WP_Query($args);