WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#30865 closed defect (bug) (wontfix)

$tax_query_defaults is hardcoded with "slug" as the field in the parse_tax_query function

Reported by: jhned Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.2
Component: Taxonomy Keywords:
Focuses: Cc:

Description

There ought to be a check for if the term is numeric, because then the field would be the term_id, not the slug.

The built-in function wp_dropdown_categories clashes with this if you use it with a custom taxonomy. The default option values are term_ids, not slugs.

So, when submitting data from wp_dropdown_categories with a custom taxonomy, you get a tax_query that has a term_id for the term, but it's trying to match it against a slug, which results in a 404 page.

Ticket #30306 will certainly help mitigate this issue, but the root cause is that slug is being assumed without checking against the value of the term.

Attachments (1)

term-numeric-test.php (532 bytes) - added by jhned 6 years ago.
Term Numeric Test

Download all attachments as: .zip

Change History (4)

@jhned
6 years ago

Term Numeric Test

#1 follow-up: @boonebgorges
6 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

when submitting data from wp_dropdown_categories with a custom taxonomy, you get a tax_query that has a term_id for the term, but it's trying to match it against a slug, which results in a 404 page.

This sounds like a legitimate issue, but we can't solve it by doing is_numeric() checks. Terms can have numeric slugs, so that a term with slug=45 would clash with a term with term_id=45. Currently, a my_custom_taxonomy=45 query var is interpreted as a slug; and changing it to be a term_id would break existing implementations. Moreover, even if we didn't have to worry about backward compatibility, doing this kind of invisible swapping-out of defaults is something that we should avoid doing except in cases where there's no possibility for ambiguity, because any bugs that arise from this sort of ambiguity are exceedingly hard to debug.

As you note, #30306 should solve the problem for the specific case of wp_dropdown_categories(). For the broader issue of ambiguous custom taxonomy query vars, one possible solution is to add support for query vars of the format my_custom_taxonomy_field=slug, which would be used to populate $tax_query_defaults in parse_tax_query(). But this seems like a lot of overhead to add if the only problem is with wp_dropdown_categories().

Marking as wontfix for the time being. If problems persist that aren't resolved by #30306, feel free to reopen with details.

#2 @boonebgorges
6 years ago

In 31006:

In wp_dropdown_categories(), allow the term field used to populate option valuesto be specified.

The new 'value_field' parameter makes it possible to set term slugs (or some
other term property) as the 'value' attribute of the option elements generated
by wp_dropdown_categories(). This additional flexibility reduces the effort
required to translate term_id to other term fields when processing form
submissions that include values from taxonomy dropdowns. See #30865 for a
use case.

Props collinsinternet.
Fixes #30306.

#3 in reply to: ↑ 1 @jhned
6 years ago

Thank you for the feedback, that makes the issue much clearer. I'll be sure to check back in to this if I find any other issues.

Note: See TracTickets for help on using tickets.