META_QUERY和TAX_QUERY一起使用的查询不能正常工作

时间:2020-07-10 作者:Fabman

我在一些类别中得到了一些帖子{ 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,
);

1 个回复
最合适的回答,由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
                        )
                )
        )
)

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post