我在一些类别中得到了一些帖子{ kids, man, woman }
, 以及每个国家的一些ACF元字段{ Japan, South Korea ... }
.
如果我用这个查询类别tax_query
它工作完美:
Array
(
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => category
[field] => slug
[terms] => Array
(
[0] => kids
)
)
)
)
这将返回中的所有帖子
kids
类别
但奇怪的是,一旦我也选择了国家(例如日本),这就增加了meta_query
, 它似乎覆盖了tax_query
我只得到了country的结果,忽略了kids
...
Array
(
[meta_query] => Array
(
[relation] => OR
[0] => Array
(
[0] => Array
(
[key] => country
[value] => Japan
[compare] => LIKE
)
)
)
[tax_query] => Array
(
[taxonomy] => category
[field] => slug
[terms] => Array
(
[0] => kids
)
)
)
这将返回
japan
.
生成该查询的代码如下:
// ADDS TAX QUERY PER TYPE
$brands_query = array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => $post_brands[\'brandList\']
);
// ADDS A META QUERY PER COUNTRY
$countries_query = array(\'relation\' => \'OR\');
foreach ($post_countries[\'countryList\'] as $value) {
$countries_query[][] = array(
\'key\' => \'country\',
\'value\' => $value,
\'compare\' => \'LIKE\',
);
}
$args = array(
\'meta_query\' => $countries_query,
\'tax_query\' => $brands_query,
);
最合适的回答,由SO网友:Az Rieil 整理而成
税务查询格式错误。它还包含数组数组(查询)
get_posts([
\'meta_query\' => [
[
\'key\' => \'country\',
\'value\' => \'Japan\',
\'compare\' => \'LIKE\'
]
],
\'tax_query\' => [
[
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => [\'kids\']
]
]
]);
阵列应如下所示:
Array
(
[meta_query] => Array
(
[relation] => OR
[0] => Array
(
[0] => Array
(
[key] => country
[value] => Japan
[compare] => LIKE
)
)
)
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => category
[field] => slug
[terms] => Array
(
[0] => kids
)
)
)
)