最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Query with meta_query and tax_query together not working properly

programmeradmin5浏览0评论

I got some post in some categories { kids, man, woman } , and some ACF meta fields per country { Japan, South Korea ... }.

If I query the categories with this tax_query it works perfectly:

Array
(
    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => category
                    [field] => slug
                    [terms] => Array
                        (
                            [0] => kids
                        )    
                )    
        )    
)

This returns all the posts in kids category.

But weirdly, once I select also country ( japan for example ) and this adds the meta_query, it seems to overwrite the tax_query and I just get the results for country ignoring the category filter for 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
                )    
        )    
)

This returns all the post for japan.

The code that generates that query is this one:

// 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,
);

I got some post in some categories { kids, man, woman } , and some ACF meta fields per country { Japan, South Korea ... }.

If I query the categories with this tax_query it works perfectly:

Array
(
    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => category
                    [field] => slug
                    [terms] => Array
                        (
                            [0] => kids
                        )    
                )    
        )    
)

This returns all the posts in kids category.

But weirdly, once I select also country ( japan for example ) and this adds the meta_query, it seems to overwrite the tax_query and I just get the results for country ignoring the category filter for 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
                )    
        )    
)

This returns all the post for japan.

The code that generates that query is this one:

// 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,
);
Share Improve this question edited Jul 10, 2020 at 12:36 Fabman asked Jul 10, 2020 at 9:53 FabmanFabman 1411 silver badge7 bronze badges 5
  • 1 There's another question here which may shed some link on things - although it does suggest you should get the result of meta_query AND tax_query. It may be worth looking at the SQL generated with e.g. Query Monitor if you can. stackoverflow/questions/24743202/… – mozboz Commented Jul 10, 2020 at 11:51
  • This is the query generated dynamically by the the code, the code "seems to work" because this is what I meant to output for the query, what it doesn't work is the query itself. For what I see on the other question on your comment, they are trying to generate an OR between the tax_query and the meta_query, and that's whats not possible with wordpress, in my case I just need an AND query that should work fine, but it doesn't. – Fabman Commented Jul 10, 2020 at 11:59
  • What do you think about looking the SQL that's generated by your query? – mozboz Commented Jul 10, 2020 at 12:30
  • I'm checking that right now, but I was avoiding it because supposedly this was easily supported natively by wordpress. But I think I'm not going to have other choice but to make the query with sql myself. – Fabman Commented Jul 10, 2020 at 12:32
  • The other question I linked to suggests that the SQL that gets built is exactly what you want, so maybe there's something else going on. – mozboz Commented Jul 10, 2020 at 12:34
Add a comment  | 

1 Answer 1

Reset to default 3

Tax query is formed wrong. It also array of arrays(queries)

get_posts([
    'meta_query'    => [
        [
        'key' => 'country',
        'value' => 'Japan',
        'compare' => 'LIKE' 
        ]   
    ],
    'tax_query' => [
        [
            'taxonomy'  => 'category',
            'field'     => 'slug',
            'terms'     => ['kids']
        ]
            
    ]
]);

the array should look like this:

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
                        )
                )
        )
)
发布评论

评论列表(0)

  1. 暂无评论