I'm trying to query a customer post type where the results may be within one of several associated terms. The issue however is that only the first term yellow-gold-jewellery
is being returned for, and all others excluded.
See my query below, by default the WP Query docs note that the operator
on the 0
entry of my tax_query
should be IN
. Despite this it is only returning products in the yellow-gold-jewellery
and neither of the latter two.
{
"posts_per_page": 9,
"status": "publish",
"tax_query": {
"0": {
"taxonomy": "jewellery_metal",
"field": "slug",
"terms": [
"yellow-gold-jewellery",
"white-gold-jewellery",
"rose-gold-jewellery"
]
},
"1": {
"taxonomy": "jewellery_availability",
"field": "slug",
"terms": [
"sold",
"unaccounted"
],
"operator": "NOT IN"
},
"relation": "AND"
},
"per_page": "9",
"post_type": "product"
}
I've tried restructuring this several ways:
- Nesting with an OR relation separate to the top level AND
- Removing the top level AND
- Manually adding operator IN to object
0
None have worked and the above seems to be exactly what is prescribed by WP docs.
I have done the obvious in checking that none of the items trip the secondary AND condition (they don't) and removing the yellow-gold-jewellery
then defers to the white-gold-jewellery
(and still not the rose-gold-jewellery
).
It's as if it's only finding the first matched term and nothing else.
Is this how this is supposed to work? Or have I misunderstood the syntax/structure?
I'm trying to query a customer post type where the results may be within one of several associated terms. The issue however is that only the first term yellow-gold-jewellery
is being returned for, and all others excluded.
See my query below, by default the WP Query docs note that the operator
on the 0
entry of my tax_query
should be IN
. Despite this it is only returning products in the yellow-gold-jewellery
and neither of the latter two.
{
"posts_per_page": 9,
"status": "publish",
"tax_query": {
"0": {
"taxonomy": "jewellery_metal",
"field": "slug",
"terms": [
"yellow-gold-jewellery",
"white-gold-jewellery",
"rose-gold-jewellery"
]
},
"1": {
"taxonomy": "jewellery_availability",
"field": "slug",
"terms": [
"sold",
"unaccounted"
],
"operator": "NOT IN"
},
"relation": "AND"
},
"per_page": "9",
"post_type": "product"
}
I've tried restructuring this several ways:
- Nesting with an OR relation separate to the top level AND
- Removing the top level AND
- Manually adding operator IN to object
0
None have worked and the above seems to be exactly what is prescribed by WP docs.
I have done the obvious in checking that none of the items trip the secondary AND condition (they don't) and removing the yellow-gold-jewellery
then defers to the white-gold-jewellery
(and still not the rose-gold-jewellery
).
It's as if it's only finding the first matched term and nothing else.
Is this how this is supposed to work? Or have I misunderstood the syntax/structure?
Share Improve this question asked Mar 5, 2022 at 19:05 jmcgroryjmcgrory 1213 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 2Related issue: Multiple relationship for multiple tax_query in WP_Query
The issue here was that mixed multi-relationship lookups are only permitted using the term_taxonomy_ids
field in the query, the above worked fine once switched like so:
$params = [
"status" => "publish",
"tax_query" => [
[
"taxonomy" => "jewellery_metal",
"field" => "term_taxonomy_id",
"terms" => [
36, // "yellow-gold-jewellery"
37, // "white-gold-jewellery"
34, // "rose-gold-jewellery"
]
],
[
"taxonomy" => "jewellery_availability",
"field" => "term_taxonomy_id",
"terms" => [
44, // "sold"
49, // "unaccounted"
],
"operator" => "NOT IN"
],
"relation" => "AND"
],
"post_type" => "product"
]
Annoying Wordpress Docs don't specify this, or at minimum suggest all queries are done on the term_taxonomy_id
if it's the only one that supports all specified functionality.
yellow-gold-jewelry
is just appearing first? Keep in mind thatNOT IN
is extremely expensive and involves copying the table so it can run the query on a temporary table in memory ( minus the bits you didn't want ). It would be much faster to list every single term in thejewellery_availability
except those two than it would be to useNOT IN
– Tom J Nowell ♦ Commented Mar 5, 2022 at 20:23operator
parameter, andper_page
andposts_per_page
are both defined – Tom J Nowell ♦ Commented Mar 5, 2022 at 20:24