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,
);
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
)
)
)
)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0