2

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,
);
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.com/questions/24743202/…
    – mozboz
    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
    Jul 10, 2020 at 11:59
  • What do you think about looking the SQL that's generated by your query?
    – mozboz
    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
    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
    Jul 10, 2020 at 12:34

1 Answer 1

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
                        )
                )
        )
)
8
  • The tax_query is already an array, and the terms inside is also an array like your example.
    – Fabman
    Jul 10, 2020 at 12:25
  • 1
    I though the same but OP is showing what the args look like from e.g. print_r
    – mozboz
    Jul 10, 2020 at 12:29
  • 2
    @mozboz I check his dump again - second part, where he displays meta & tax query in one array is wrong in tax part
    – Az Rieil
    Jul 10, 2020 at 13:59
  • 2
    @Fabman check my answer and you upd. code again - you missing inside array for taxonomy query. tax query, as meta query, must be array of array of queries. even if it only one query. Or just wrap $brands_query into additional array
    – Az Rieil
    Jul 10, 2020 at 14:01
  • 2
    Your answer was perfect, I just edited it so the structure looks the same way the question was presented ( with a print_r of the query ). Thank you very much for your help!
    – Fabman
    Jul 10, 2020 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.