0

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:

  1. Nesting with an OR relation separate to the top level AND
  2. Removing the top level AND
  3. 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?

7
  • your query appears to be JSON not PHP, and the PHP code that takes those parameters and attempts to use them is missing. Are you sure that it's not including the other terms? Could it be they're on the next page and the yellow-gold-jewelry is just appearing first? Keep in mind that NOT 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 the jewellery_availability except those two than it would be to use NOT IN
    – Tom J Nowell
    Commented Mar 5, 2022 at 20:23
  • I also see your first clause has no operator parameter, and per_page and posts_per_page are both defined
    – Tom J Nowell
    Commented Mar 5, 2022 at 20:24
  • @TomJNowell it's just JSON because that's the format I've outputted it in for debugging. I appreciate the tips on performance, I'll implement them separately. Yes I am not lying, and have mentioned how I've tested multiple conditions to make sure that the yellow-gold-jewellery isn't some sticking point (querying just the last two only returning the first of the last two as well).
    – jmcgrory
    Commented Mar 5, 2022 at 20:28
  • @TomJNowell as for the lack of operator parameter, I did mention that the WP docs lists the default as IN, I also mention that I manually included it in my testing to similarly little avail. I haven't tried removing the ..._page properties on the query, I'll see if they're the sticking point.
    – jmcgrory
    Commented Mar 5, 2022 at 20:30
  • @TomJNowell removing the page related parts does absolutely nothing to help unfortunately.
    – jmcgrory
    Commented Mar 5, 2022 at 20:55

1 Answer 1

2

Related 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.

Your Answer

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

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