meta_query
是元条款的数组。例如:
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'state',
'value' => 'Wisconsin',
),
array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
) );
您可以使用关联数组,并为每个meta子句添加一个键:
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'state_clause' => array(
'key' => 'state',
'value' => 'Wisconsin',
),
'city_clause' => array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
) );
然后,您可以在order_by
参数中使用以下键:
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'state_clause' => array(
'key' => 'state',
'value' => 'Wisconsin',
),
'city_clause' => array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
'orderby' => 'city_clause', // Results will be ordered by 'city' meta values.
) );
或更多条款:
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'state_clause' => array(
'key' => 'state',
'value' => 'Wisconsin',
),
'city_clause' => array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'city_clause' => 'ASC',
'state_clause' => 'DESC',
),
) );
摘自Make WordPres Core博客中的帖子示例。