Custom Tax Query by name/slug only appears to work
-
Hi! So I noticed inconsistent results with a custom tax query where some taxonomies were filtering as expected, and others were being completely ignored.
I did all the debugging myself, and came to the below conclusion.
In the function
relevanssi_get_term_in()
(in lib/search-tax-query.php), the following line:$term = get_term_by( 'name', $name, $taxonomy );
expects
$name
to be a ‘name’, never giving it the option of being a ‘slug’, despite comments suggesting it does. Then, when I changed my arguments to use ‘name’ instead of ‘slug’, it still didn’t work, because whenrelevanssi_term_tax_id_from_row()
runs its query, it grabs its type from$row['field']
, which was of course now set to ‘name’. The problem here is that it looks at this type (‘name’) against the terms created on this line ofrelevanssi_get_term_in()
(line 360):$name = sanitize_title( $name );
So it’s creating slugs from names and searching the database for terms whose names are these slugs. To be fair, WordPress is really inconsistent with its naming! (With its slugging?)
The funny thing is that this, of course, works just fine if the term only has one word.
And that explained my inconsistency, and why it seems to have worked when it was introduced in 3.5.9. My taxonomy term of “Healthcare” worked great. “Life Sciences” did not. You can imagine how much of a headscratcher that was until I dug in!
So to replicate this: Create a taxonomy term with two or more words so that the slug does not match the name. Then do a custom tax_query by ‘slug’. (Or even by ‘name’.)
Once I found the issue, I discovered it worked to use ‘slug’ but pass in names. But I didn’t like doing that, so I instead worked around it by using IDs. That’s working well, so I don’t need an immediate fix. But I think it would be great to get that truly working!
Let me know if you’re able to replicate or if you need more info from me.
- The topic ‘Custom Tax Query by name/slug only appears to work’ is closed to new replies.