Repository navigation
Taxonomy: Introduce allow_multiple_terms for single-term taxonomies. - #14352
abhishekfdd wants to merge 1 commit into
Conversation
Adds an `allow_multiple_terms` argument to `register_taxonomy()`, defaulting to `true`. When `false`: - The default meta box renders radio buttons (hierarchical) or a dropdown (non-hierarchical) via the new `post_single_term_meta_box()`. - Submitted values are sanitized to at most one term ID by the new `taxonomy_meta_box_sanitize_cb_single_term()`. - `wp_set_object_terms()` keeps only the last term and treats `$append` as a replace, so REST, WP-CLI and direct callers honor the constraint. - Quick Edit renders the matching single-select controls, and `get_inline_data()` exposes the current term ID so `inline-edit-post.js` can preselect it. Without this, Quick Edit opened with nothing selected and saving the row (even to change only the title) removed the post's term. Props bastho, umarkulachi. See #64991.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
Thanks for this PR @abhishekfdd |
Adds an
allow_multiple_termsargument toregister_taxonomy()(defaulttrue). Setting it tofalsegives a taxonomy single-term behavior, similar to post formats.When
allow_multiple_termsisfalse:post_single_term_meta_box(). It renders radio buttons for hierarchical taxonomies and a<select>for non-hierarchical ones. A "None" option appears unless adefault_termis set. A custommeta_box_cbstill wins.taxonomy_meta_box_sanitize_cb_single_term()reduces the submitted value to at most one term ID.wp_set_object_terms()keeps only the last term and treats$appendas a replace. REST, WP-CLI and direct callers all follow the rule, not just the admin UI.get_inline_data()now outputs apost_single_termelement with the current term ID, andinline-edit-post.jsuses it to preselect the radio or dropdown.What changed since 64991.diff
@umarkulachi reported that Quick Edit didn't preselect the saved term. Testing the first patch showed the problem was wider:
get_inline_data()only exposed term IDs in a form the JS matched against:checkbox, and exposed term names for flat taxonomies, but the dropdown is keyed by ID.Both are fixed here, and the patch now includes unit tests (the first patch had none).
Testing
Test plugin (term names deliberately don't match post statuses):
Quick Edit round trip on Posts > All Posts (automated in Playwright, then checked against the DB with WP-CLI):
Regression checks: Quick Edit for core Categories (checkboxes) and Tags (textarea) still preselects correctly, with no JS errors. The classic meta box restores the saved term.
Unit tests:
tests/phpunit/tests/taxonomy/allowMultipleTerms.php(15 tests). It covers default wiring, the custommeta_box_cbprecedence, thewp_set_object_terms()cap and the$appenddowngrade, the sanitize callback, and theget_inline_data()output. Theget_inline_data()tests fail without the fix.--group taxonomypasses.Not in this PR (open questions on the ticket)
allow_multiple_termsin the REST taxonomies schema first).allow_multiple_terms.Trac ticket: https://core.trac.wordpress.org/ticket/64991
Use of AI Tools
AI assistance: Yes
Tool(s): Claude
Model(s): Claude Opus 5.5
Used for: Reproducing the Quick Edit bug in a scripted browser test, drafting the
get_inline_data()/inline-edit-post.jsfix and the unit tests, and drafting this description.This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.