Question

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,
);
Was it helpful?

Solution

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
                        )
                )
        )
)
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top