Opened 8 months ago
Last modified 4 weeks ago
#64991 new feature request
New param "allow_multiple_terms" in `register_taxonomy` for single term restriction
| Reported by: | bastho | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Taxonomy | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests needs-testing has-test-info |
| Cc: | Focuses: | ui |
Description
The register_taxonomy function is based on the two main taxonomies: categories and tags.
Custom taxonomies can only have those two behaviors: hierarchical terms, like categories or non hierarchical terms like tags.
At this time, when a plugin adds a custom taxonomy, the generated UI is:
- checkboxes for category like taxonomies
- multiple select for tag like taxonomies
If a plugin needs to exclusive terms, the UI has to be hidden and a custom box added which causes UI inconsistency between plugins.
It would be very useful to natively allow single term behavior, just like post-format does.
In this case, the UI could be a simple select or radio buttons (preference for select).
I suggest a allow_multiple_terms parameter, with default value true
Setting it to false would make use the simple <select> field.
Caveats:
Need to manage: single edit page, quick-edit and bulk-edit.
Attachments (5)
Change History (10)
#2
@
4 months ago
I tested the patch on a fresh WordPress development environment (Windows 10, Classic Editor, Test Single Taxonomy plugin).
What worked:
The single-term taxonomy UI is displayed as radio buttons.
Saving from both the post editor and Quick Edit works.
The selected term is displayed correctly in the Posts list.
Reopening the full editor correctly restores the saved term.
Issue found:
After changing the Book Status in Quick Edit (for example, from "Published" to "Draft") and clicking Update, the Posts list correctly displays Draft.
However, reopening Quick Edit for the same post does not preselect the saved term. All radio buttons are unselected, even though the full editor and the Posts list both show the correct saved value.
Result: The patch works partially, but Quick Edit does not restore the current single taxonomy selection.
This ticket was mentioned in PR #14352 on WordPress/wordpress-develop by @abhishekfdd.
4 weeks ago
#3
Adds an allow_multiple_terms argument to register_taxonomy() (default true). Setting it to false gives a taxonomy single-term behavior, similar to post formats.
When allow_multiple_terms is false:
- Meta box: the default meta box becomes
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. - Sanitizing:
taxonomy_meta_box_sanitize_cb_single_term()reduces the submitted value to at most one term ID. - Enforcement:
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. - Quick Edit: renders the same single-select controls.
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:
- Nothing was preselected even on a fresh page load, for both radios and the dropdown.
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. - That meant data loss: opening Quick Edit and changing only the title removed the post's term, because the form submitted the empty sentinel.
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):
add_action( 'init', function () { register_taxonomy( 'book_status', 'post', array( 'label' => 'Book Status', 'hierarchical' => true, 'show_admin_column' => true, 'allow_multiple_terms' => false ) ); register_taxonomy( 'book_shelf', 'post', array( 'label' => 'Book Shelf', 'hierarchical' => false, 'show_admin_column' => true, 'allow_multiple_terms' => false ) ); } ); add_filter( 'use_block_editor_for_post', '__return_false' );
Quick Edit round trip on Posts > All Posts (automated in Playwright, then checked against the DB with WP-CLI):
| Step | Before | After |
|---|---|---|
| Open Quick Edit on fresh load (Hardcover / History saved) | nothing selected / "None" | Hardcover / History |
| Save Paperback / Science, reopen without reload | nothing selected / "None" | Paperback / Science |
| Change only the title, Update | both terms removed | terms kept |
| Choose None, Update, reopen | n/a | None |
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 custom meta_box_cb precedence, the wp_set_object_terms() cap and the $append downgrade, the sanitize callback, and the get_inline_data() output. The get_inline_data() tests fail without the fix. --group taxonomy passes.
Not in this PR (open questions on the ticket)
- Block editor sidebar (lives in Gutenberg; needs
allow_multiple_termsin the REST taxonomies schema first). - REST schema exposure of
allow_multiple_terms. - Bulk Edit has no per-taxonomy controls for these taxonomies. The server-side cap still applies.
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.js fix and the unit tests, and drafting this description.
#4
@
4 weeks ago
- Keywords has-test-info added
Thanks for testing, @umarkulachi, and sorry this sat for so long.
I reproduced it, and it was wider than the report. With 64991.diff, Quick Edit didn't preselect the saved term even on a fresh page load, for both the radio buttons (hierarchical) and the dropdown (non-hierarchical). Worse, opening Quick Edit and changing only the title removed the post's terms, because the form submitted the empty sentinel value.
Cause: get_inline_data() exposed term IDs only in the form inline-edit-post.js matches against :checkbox, and exposed term names for flat taxonomies, while the dropdown is keyed by ID.
64991.2.diff fixes this:
get_inline_data()now outputs apost_single_termelement with the current term ID (0 when none) for taxonomies withallow_multiple_terms => false.inline-edit-post.jsuses it to preselect the radio or dropdown.- Adds unit tests in
tests/phpunit/tests/taxonomy/allowMultipleTerms.php(15 tests) covering the defaults wiring, custommeta_box_cbprecedence, thewp_set_object_terms()cap and$appenddowngrade, the sanitize callback, andget_inline_data(). The inline data tests fail without the fix. - Updates the
@sincetags to 7.2.0.
Results on trunk (checked against the database with WP-CLI after each save):
| Step | 64991.diff | 64991.2.diff |
|---|---|---|
| Open Quick Edit on fresh load (Hardcover / History saved) | nothing selected / "None" | Hardcover / History |
| Save Paperback / Science, reopen without reload | nothing selected / "None" | Paperback / Science |
| Change only the title, Update | both terms removed | terms kept |
| Choose None, Update, reopen | n/a | None |
Core Categories and Tags in Quick Edit still preselect correctly, with no JS errors.
When retesting, it helps to use term names that don't match post statuses (I used Hardcover / Paperback / Ebook), so a term change can't be confused with the post Status dropdown.
PR for review: https://github.com/WordPress/wordpress-develop/pull/XXXX
Still open from comment:1: the block editor sidebar (needs allow_multiple_terms in the REST taxonomies schema first, then a Gutenberg change), and whether to expose it in REST in this patch or a follow-up. Feedback welcome on either.
@rejaulalomkhan commented on PR #14352:
4 weeks ago
#5
Thanks for this PR @abhishekfdd
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)


Thanks for opening this, @bastho. Attaching a first patch (64991.diff) that covers the feature end-to-end, along with the reasoning behind a few decisions that came up while writing it. Happy to iterate on any of them.
What the patch changes
WP_Taxonomy — adds an $allow_multiple_terms property (default true) and accepts it in set_props(). When false and no custom meta_box_cb is supplied, the default meta box callback is switched to a new post_single_term_meta_box() and the sanitize callback to a new taxonomy_meta_box_sanitize_cb_single_term().
register_taxonomy() docblock — documents the new argument and a new @since 7.1.0 tag for the arg introduction.
post_single_term_meta_box() (new, in wp-admin/includes/meta-boxes.php) — renders radio buttons for hierarchical taxonomies and a <select> (via wp_dropdown_categories()) for non-hierarchical ones. A "None" option is offered when no default_term is configured.
taxonomy_meta_box_sanitize_cb_single_term() (new, in wp-admin/includes/post.php) — normalizes the submitted array to at most one term ID.
wp_set_object_terms() — enforces the cap at the shared assignment path, so REST, WP-CLI, and any direct caller honor the constraint — not just the admin UI. When $append is passed for a single-term taxonomy, the append is downgraded to a replace to avoid quietly stacking terms.
Quick Edit (WP_Posts_List_Table) — for hierarchical taxonomies, renders a radio list instead of wp_terms_checklist(); for non-hierarchical, renders a dropdown instead of the tags <textarea>. Bulk Edit already excludes per-taxonomy controls for non-category taxonomies, so the server-side cap in (5) is what protects bulk operations.
Design decisions worth flagging
Argument name: allow_multiple_terms. I kept the name you proposed. single_value reads cleanly as a property but pairs awkwardly with a default of true ('single_value' => true to mean "allow many" is a double negative). A meta_box_style enum was tempting for extensibility, but it conflates UI shape with cardinality — the server-side cap has to happen regardless of which UI is used, so a boolean flag that governs both defaults matches the semantics better.
Default is true. Every existing register_taxonomy() call is unaffected.
Enforcement lives in wp_set_object_terms(), not just the metabox sanitizer. This was the biggest design call. The alternative — only capping in the sanitize callback — would leave REST (POST /wp/v2/posts with a tax array of many IDs), WP-CLI, and any plugin calling wp_set_post_terms() free to bypass the constraint. Centralizing it at the assignment path matches how default_term and other taxonomy-level semantics are enforced, and keeps the contract consistent for API consumers.
"Last value wins" when multiple terms are submitted. This matches post-format semantics and means that if a form somehow submits two radio values (e.g. a custom client), the most recent one wins. Open to a stricter WP_Error instead if that's preferred.
Custom meta_box_cb precedence. If a caller supplies their own meta_box_cb, we don't override it even when allow_multiple_terms => false — the custom UI is assumed to know what it's doing. The server-side cap still applies, so a misbehaving custom UI can't save multiple terms.
Gutenberg / block editor. Out of scope for Core Trac — belongs in the gutenberg repo. The useEntityProp-backed PostTaxonomies component would need to read allow_multiple_terms (ideally exposed via the taxonomies REST schema) and render a <RadioControl> / <SelectControl> in place of the flat/hierarchical token field. Worth a companion ticket once the Core API shape is settled here. Also not yet exposed in the REST taxonomies endpoint — I didn't add it to WP_REST_Taxonomies_Controller::get_item_schema() in this patch because the API surface felt worth agreeing on first, but it's a natural follow-up.
Bulk Edit. The existing UI already suppresses per-taxonomy controls in bulk mode (! $bulk), and the cap in wp_set_object_terms() means bulk-updating many posts to a single term still works correctly. No per-row bulk UI change needed, though an explicit bulk-edit dropdown for single-term taxonomies could be a follow-up enhancement.
Open questions
Argument naming — fine as-is, or would committers prefer an alternative?
REST schema exposure — add allow_multiple_terms to the taxonomies endpoint schema in this patch or a follow-up?
default_term interaction — when allow_multiple_terms => false and a default_term is set, I'm suppressing the "None" option so the UI always reflects a valid selection. Happy to make that configurable if there's a use case for allowing explicit clearing.
Unit tests — haven't added them in this first pass; will follow up in a separate patch once the API shape is agreed, focused on wp_set_object_terms() cap behavior (including the $append downgrade), the new sanitize callback, and register_taxonomy() defaults wiring.