ここの例に基づいて https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/
クエリを変更したい
$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') );
state
が 'Wisconsin' OR timezone
が 'central'の投稿をすべて取得できるようにするにはORDER BY population
DESC
特定の比較操作を使用してmeta_queriesのグループを作成できます。また、単一のカスタムフィールドに基づいて注文するので、注文宣言を単一のメタフィールド専用にすることができます。そう:
$q = new WP_Query(
array(
'meta_key' => 'population', //setting the meta_key which will be used to order
'orderby' => 'meta_value', //if the meta_key (population) is numeric use meta_value_num instead
'order' => 'DESC', //setting order direction
'meta_query' => array(
'relation' => 'AND', //setting relation between queries group
array(
'relation' => 'OR', //setting relation between this inside query
array(
'key' => 'state',
'value' => 'Wisconsin',
),
array(
'key' => 'timezone',
'value' => 'central',
)
),
array(
'key' => 'city',
'compare' => 'EXISTS',
)
)
)
);
これは次のようになります。
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
'state_clause' => array(
'key' => 'state',
'value' => array( 'Wisconsin' ), //allowed values
'compare' => 'IN' // state must be in array above
),
'state_clause' => array(
'key' => 'timezone',
'value' => 'central',
'compare' => '='
),
),
'city_clause' => array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'city_clause' => 'DESC',
),
) );