diff --git a/src/js/_enqueues/admin/inline-edit-post.js b/src/js/_enqueues/admin/inline-edit-post.js
index aaf4bcc5bc3ec..c6fbfea560cf9 100644
--- a/src/js/_enqueues/admin/inline-edit-post.js
+++ b/src/js/_enqueues/admin/inline-edit-post.js
@@ -411,6 +411,19 @@ window.wp = window.wp || {};
}
});
+ /**
+ * Preselects the current term for single-term taxonomies, which render
+ * radio buttons (hierarchical) or a dropdown (non-hierarchical).
+ * A value of 0 selects the "None" option when one is offered.
+ */
+ $( '.post_single_term', rowData ).each( function() {
+ var taxname = $( this ).attr( 'id' ).replace( '_' + id, '' ),
+ termId = $( this ).text().trim() || '0';
+
+ $( 'ul.' + taxname + '-checklist :radio', editRow ).val( [ termId ] );
+ $( 'select[name="tax_input[' + taxname + '][]"]', editRow ).val( termId );
+ });
+
/**
* Gets all the taxonomies for live auto-fill suggestions when typing the name
* of a tag.
diff --git a/src/wp-admin/includes/class-wp-posts-list-table.php b/src/wp-admin/includes/class-wp-posts-list-table.php
index d461aacd312ae..e9e016acce6fc 100644
--- a/src/wp-admin/includes/class-wp-posts-list-table.php
+++ b/src/wp-admin/includes/class-wp-posts-list-table.php
@@ -1900,10 +1900,56 @@ public function inline_edit() {
labels->name ); ?>
-
-
- $taxonomy->name ) ); ?>
-
+ name )
+ ? 'post_category[]'
+ : 'tax_input[' . esc_attr( $taxonomy->name ) . '][]';
+
+ $single_term = isset( $taxonomy->allow_multiple_terms ) && ! $taxonomy->allow_multiple_terms;
+ ?>
+
+
+
+
+
+ $taxonomy->name ) ); ?>
+
+
@@ -1996,13 +2042,37 @@ public function inline_edit() {
cap->assign_terms ) ) : ?>
- name ); ?>
+ name );
+ $single_term = isset( $taxonomy->allow_multiple_terms ) && ! $taxonomy->allow_multiple_terms;
+ ?>
diff --git a/src/wp-admin/includes/meta-boxes.php b/src/wp-admin/includes/meta-boxes.php
index 9149f50daadd8..e9fc290eba729 100644
--- a/src/wp-admin/includes/meta-boxes.php
+++ b/src/wp-admin/includes/meta-boxes.php
@@ -548,6 +548,126 @@ function post_format_meta_box( $post, $box ) {
endif;
}
+/**
+ * Displays a single-term taxonomy meta box.
+ *
+ * Used as the default meta box callback for taxonomies registered with
+ * `allow_multiple_terms` set to false. Renders radio buttons for hierarchical
+ * taxonomies and a dropdown for non-hierarchical ones, so that at most one
+ * term can be selected.
+ *
+ * @since 7.2.0
+ *
+ * @param WP_Post $post Current post object.
+ * @param array $box {
+ * Single-term meta box arguments.
+ *
+ * @type string $id Meta box 'id' attribute.
+ * @type string $title Meta box title.
+ * @type callable $callback Meta box display callback.
+ * @type array $args {
+ * Extra meta box arguments.
+ *
+ * @type string $taxonomy Taxonomy name.
+ * }
+ * }
+ */
+function post_single_term_meta_box( $post, $box ) {
+ $defaults = array( 'taxonomy' => 'category' );
+ if ( ! isset( $box['args'] ) || ! is_array( $box['args'] ) ) {
+ $args = array();
+ } else {
+ $args = $box['args'];
+ }
+ $parsed_args = wp_parse_args( $args, $defaults );
+ $tax_name = esc_attr( $parsed_args['taxonomy'] );
+ $taxonomy = get_taxonomy( $parsed_args['taxonomy'] );
+
+ if ( ! $taxonomy ) {
+ return;
+ }
+
+ // Input field name matches what the core save path expects.
+ $name = ( 'category' === $tax_name ) ? 'post_category' : 'tax_input[' . $tax_name . ']';
+
+ // Current term for this post, if any.
+ $current_terms = get_the_terms( $post->ID, $tax_name );
+ $current_id = 0;
+ if ( is_array( $current_terms ) && ! empty( $current_terms ) ) {
+ $current_term = reset( $current_terms );
+ $current_id = (int) $current_term->term_id;
+ }
+
+ // A "None" option is only offered when no default term is configured.
+ $has_default_term = ! empty( $taxonomy->default_term );
+
+ ?>
+
+ ';
+
+ if ( $taxonomy->hierarchical ) :
+ $terms = get_terms(
+ array(
+ 'taxonomy' => $tax_name,
+ 'hide_empty' => false,
+ )
+ );
+
+ if ( is_wp_error( $terms ) || empty( $terms ) ) {
+ echo '
' . esc_html( $taxonomy->labels->not_found ) . '
';
+ } else {
+ ?>
+
+ $tax_name,
+ 'hide_empty' => false,
+ 'hierarchical' => false,
+ 'orderby' => 'name',
+ 'name' => $name . '[]',
+ 'id' => 'taxonomy-' . $tax_name . '-select',
+ 'selected' => $current_id,
+ 'show_option_none' => $has_default_term ? '' : __( 'None' ),
+ 'option_none_value' => 0,
+ 'value_field' => 'term_id',
+ );
+ ?>
+
+
+
+
+ 0 ) {
+ $clean[] = $term;
+ }
+ }
+
+ if ( empty( $clean ) ) {
+ return array();
+ }
+
+ // Keep only the last submitted term to honor the single-term constraint.
+ return array( end( $clean ) );
+}
+
/**
* Sanitizes POST values from an input taxonomy metabox.
*
diff --git a/src/wp-admin/includes/template.php b/src/wp-admin/includes/template.php
index c01d9ffaf8ad0..48ef72136dfea 100644
--- a/src/wp-admin/includes/template.php
+++ b/src/wp-admin/includes/template.php
@@ -354,7 +354,19 @@ function get_inline_data( $post ) {
continue;
}
- if ( $taxonomy->hierarchical ) {
+ if ( ! $taxonomy->allow_multiple_terms ) {
+
+ /*
+ * Single-term taxonomies render radio buttons (hierarchical) or a dropdown
+ * (non-hierarchical) in Quick Edit, both keyed by term ID, so expose the
+ * current term ID for inline-edit-post.js to preselect.
+ */
+ $terms = get_the_terms( $post->ID, $taxonomy_name );
+ $term_id = ( is_array( $terms ) && ! empty( $terms ) ) ? (int) reset( $terms )->term_id : 0;
+
+ echo '' . $term_id . '
';
+
+ } elseif ( $taxonomy->hierarchical ) {
$terms = get_object_term_cache( $post->ID, $taxonomy_name );
if ( false === $terms ) {
diff --git a/src/wp-includes/class-wp-taxonomy.php b/src/wp-includes/class-wp-taxonomy.php
index f44ce34a11def..5990b5bcf3ba3 100644
--- a/src/wp-includes/class-wp-taxonomy.php
+++ b/src/wp-includes/class-wp-taxonomy.php
@@ -133,6 +133,19 @@ final class WP_Taxonomy {
*/
public $show_admin_column = false;
+ /**
+ * Whether objects of this taxonomy can be associated with more than one term.
+ *
+ * When set to false, core admin UIs render a single-select control (radio buttons for
+ * hierarchical taxonomies, a dropdown for non-hierarchical ones) and term assignment
+ * is capped at one term per object across all entry points, including the REST API
+ * and wp_set_object_terms(). Default true.
+ *
+ * @since 7.2.0
+ * @var bool
+ */
+ public $allow_multiple_terms = true;
+
/**
* The callback function for the meta box display.
*
@@ -357,6 +370,7 @@ public function set_props( $object_type, $args ) {
'show_tagcloud' => null,
'show_in_quick_edit' => null,
'show_admin_column' => false,
+ 'allow_multiple_terms' => true,
'meta_box_cb' => null,
'meta_box_sanitize_cb' => null,
'capabilities' => array(),
@@ -440,7 +454,9 @@ public function set_props( $object_type, $args ) {
// If not set, use the default meta box.
if ( null === $args['meta_box_cb'] ) {
- if ( $args['hierarchical'] ) {
+ if ( ! $args['allow_multiple_terms'] ) {
+ $args['meta_box_cb'] = 'post_single_term_meta_box';
+ } elseif ( $args['hierarchical'] ) {
$args['meta_box_cb'] = 'post_categories_meta_box';
} else {
$args['meta_box_cb'] = 'post_tags_meta_box';
@@ -452,6 +468,10 @@ public function set_props( $object_type, $args ) {
// Default meta box sanitization callback depends on the value of 'meta_box_cb'.
if ( null === $args['meta_box_sanitize_cb'] ) {
switch ( $args['meta_box_cb'] ) {
+ case 'post_single_term_meta_box':
+ $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_single_term';
+ break;
+
case 'post_categories_meta_box':
$args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_checkboxes';
break;
diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php
index ea13e99271cf0..c7f3ea72d0df1 100644
--- a/src/wp-includes/taxonomy.php
+++ b/src/wp-includes/taxonomy.php
@@ -430,6 +430,7 @@ function is_taxonomy_hierarchical( $taxonomy ) {
* @since 5.4.0 Added the registered taxonomy object as a return value.
* @since 5.5.0 Introduced `default_term` argument.
* @since 5.9.0 Introduced `rest_namespace` argument.
+ * @since 7.2.0 Introduced `allow_multiple_terms` argument.
*
* @global WP_Taxonomy[] $wp_taxonomies Registered taxonomies.
*
@@ -471,10 +472,18 @@ function is_taxonomy_hierarchical( $taxonomy ) {
* the default is inherited from `$show_ui` (default true).
* @type bool $show_admin_column Whether to display a column for the taxonomy on its post type listing
* screens. Default false.
+ * @type bool $allow_multiple_terms Whether objects can be assigned more than one term from this taxonomy.
+ * When false, the default meta box renders a single-select control
+ * (radios for hierarchical taxonomies, a dropdown for non-hierarchical
+ * ones) and term assignment is capped at one term per object across all
+ * entry points, including the REST API and wp_set_object_terms().
+ * Ignored when a custom `$meta_box_cb` is supplied, though the
+ * server-side cap still applies. Default true.
* @type bool|callable $meta_box_cb Provide a callback function for the meta box display. If not set,
* post_categories_meta_box() is used for hierarchical taxonomies, and
- * post_tags_meta_box() is used for non-hierarchical. If false, no meta
- * box is shown.
+ * post_tags_meta_box() is used for non-hierarchical. If
+ * `$allow_multiple_terms` is false, post_single_term_meta_box() is used
+ * instead. If false, no meta box is shown.
* @type callable $meta_box_sanitize_cb Callback function for sanitizing taxonomy data saved from a meta
* box. If no callback is defined, an appropriate one is determined
* based on the value of `$meta_box_cb`.
@@ -2979,6 +2988,21 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
$terms = array( $terms );
}
+ /*
+ * For single-term taxonomies (registered with allow_multiple_terms => false),
+ * keep only the last submitted term so that every entry point -- admin, REST API,
+ * WP-CLI, direct callers -- honors the cardinality constraint. When $append is
+ * true for a single-term taxonomy, appending would otherwise stack terms, so the
+ * append request is downgraded to a replace with the supplied term.
+ */
+ $taxonomy_object = get_taxonomy( $taxonomy );
+ if ( $taxonomy_object && isset( $taxonomy_object->allow_multiple_terms ) && ! $taxonomy_object->allow_multiple_terms ) {
+ if ( count( $terms ) > 1 ) {
+ $terms = array( end( $terms ) );
+ }
+ $append = false;
+ }
+
if ( ! $append ) {
$old_tt_ids = wp_get_object_terms(
$object_id,
diff --git a/tests/phpunit/tests/taxonomy/allowMultipleTerms.php b/tests/phpunit/tests/taxonomy/allowMultipleTerms.php
new file mode 100644
index 0000000000000..a6cfc6922336c
--- /dev/null
+++ b/tests/phpunit/tests/taxonomy/allowMultipleTerms.php
@@ -0,0 +1,219 @@
+post->create();
+ }
+
+ public function set_up() {
+ parent::set_up();
+
+ require_once ABSPATH . 'wp-admin/includes/meta-boxes.php';
+ require_once ABSPATH . 'wp-admin/includes/post.php';
+ require_once ABSPATH . 'wp-admin/includes/template.php';
+
+ register_taxonomy(
+ 'wptests_single_h',
+ 'post',
+ array(
+ 'hierarchical' => true,
+ 'allow_multiple_terms' => false,
+ )
+ );
+
+ register_taxonomy(
+ 'wptests_single_flat',
+ 'post',
+ array(
+ 'hierarchical' => false,
+ 'allow_multiple_terms' => false,
+ )
+ );
+ }
+
+ public function tear_down() {
+ unregister_taxonomy( 'wptests_single_h' );
+ unregister_taxonomy( 'wptests_single_flat' );
+ parent::tear_down();
+ }
+
+ /**
+ * @covers WP_Taxonomy::set_props
+ */
+ public function test_allow_multiple_terms_defaults_to_true() {
+ register_taxonomy( 'wptests_default', 'post' );
+ $taxonomy = get_taxonomy( 'wptests_default' );
+ unregister_taxonomy( 'wptests_default' );
+
+ $this->assertTrue( $taxonomy->allow_multiple_terms );
+ $this->assertSame( 'post_tags_meta_box', $taxonomy->meta_box_cb );
+ }
+
+ /**
+ * @covers WP_Taxonomy::set_props
+ *
+ * @dataProvider data_single_term_taxonomies
+ *
+ * @param string $taxonomy Taxonomy name.
+ */
+ public function test_single_term_taxonomy_uses_single_term_callbacks( $taxonomy ) {
+ $tax = get_taxonomy( $taxonomy );
+
+ $this->assertFalse( $tax->allow_multiple_terms );
+ $this->assertSame( 'post_single_term_meta_box', $tax->meta_box_cb );
+ $this->assertSame( 'taxonomy_meta_box_sanitize_cb_single_term', $tax->meta_box_sanitize_cb );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_single_term_taxonomies() {
+ return array(
+ 'hierarchical' => array( 'wptests_single_h' ),
+ 'non-hierarchical' => array( 'wptests_single_flat' ),
+ );
+ }
+
+ /**
+ * @covers WP_Taxonomy::set_props
+ */
+ public function test_custom_meta_box_cb_is_not_overridden() {
+ register_taxonomy(
+ 'wptests_custom_cb',
+ 'post',
+ array(
+ 'allow_multiple_terms' => false,
+ 'meta_box_cb' => 'post_categories_meta_box',
+ )
+ );
+ $taxonomy = get_taxonomy( 'wptests_custom_cb' );
+ unregister_taxonomy( 'wptests_custom_cb' );
+
+ $this->assertSame( 'post_categories_meta_box', $taxonomy->meta_box_cb );
+ $this->assertSame( 'taxonomy_meta_box_sanitize_cb_checkboxes', $taxonomy->meta_box_sanitize_cb );
+ }
+
+ /**
+ * @covers ::wp_set_object_terms
+ */
+ public function test_wp_set_object_terms_keeps_only_last_term() {
+ $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_single_h' ) );
+
+ wp_set_object_terms( self::$post_id, $terms, 'wptests_single_h' );
+
+ $this->assertSame(
+ array( $terms[2] ),
+ wp_get_object_terms( self::$post_id, 'wptests_single_h', array( 'fields' => 'ids' ) )
+ );
+ }
+
+ /**
+ * @covers ::wp_set_object_terms
+ */
+ public function test_wp_set_object_terms_append_replaces_existing_term() {
+ $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_single_h' ) );
+
+ wp_set_object_terms( self::$post_id, $terms[0], 'wptests_single_h' );
+ wp_set_object_terms( self::$post_id, $terms[1], 'wptests_single_h', true );
+
+ $this->assertSame(
+ array( $terms[1] ),
+ wp_get_object_terms( self::$post_id, 'wptests_single_h', array( 'fields' => 'ids' ) )
+ );
+ }
+
+ /**
+ * @covers ::wp_set_object_terms
+ */
+ public function test_wp_set_object_terms_is_unchanged_for_multi_term_taxonomies() {
+ $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'category' ) );
+
+ wp_set_object_terms( self::$post_id, $terms, 'category' );
+
+ $this->assertSameSets(
+ $terms,
+ wp_get_object_terms( self::$post_id, 'category', array( 'fields' => 'ids' ) )
+ );
+ }
+
+ /**
+ * @covers ::taxonomy_meta_box_sanitize_cb_single_term
+ *
+ * @dataProvider data_sanitize_cb_single_term
+ *
+ * @param mixed $input Raw submitted value.
+ * @param int[] $expected Expected sanitized value.
+ */
+ public function test_sanitize_cb_single_term( $input, $expected ) {
+ $this->assertSame( $expected, taxonomy_meta_box_sanitize_cb_single_term( 'wptests_single_h', $input ) );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_sanitize_cb_single_term() {
+ return array(
+ 'sentinel only' => array( array( '0' ), array() ),
+ 'sentinel and a term' => array( array( '0', '12' ), array( 12 ) ),
+ 'multiple terms, last one' => array( array( '0', '12', '15' ), array( 15 ) ),
+ 'scalar value' => array( '7', array( 7 ) ),
+ 'non-numeric junk' => array( array( 'abc', '-3' ), array() ),
+ );
+ }
+
+ /**
+ * Quick Edit preselects from the inline data, so it must expose the current
+ * term ID for single-term taxonomies, including after an inline save.
+ *
+ * @covers ::get_inline_data
+ *
+ * @dataProvider data_single_term_taxonomies
+ *
+ * @param string $taxonomy Taxonomy name.
+ */
+ public function test_get_inline_data_exposes_single_term_id( $taxonomy ) {
+ wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+
+ $term_id = self::factory()->term->create( array( 'taxonomy' => $taxonomy ) );
+ wp_set_object_terms( self::$post_id, $term_id, $taxonomy );
+
+ $expected = '' . $term_id . '
';
+
+ $output = get_echo( 'get_inline_data', array( get_post( self::$post_id ) ) );
+
+ $this->assertStringContainsString( $expected, $output );
+ $this->assertStringNotContainsString( 'class="post_category" id="' . $taxonomy . '_', $output );
+ $this->assertStringNotContainsString( 'class="tags_input" id="' . $taxonomy . '_', $output );
+ }
+
+ /**
+ * @covers ::get_inline_data
+ */
+ public function test_get_inline_data_exposes_zero_when_no_term_is_set() {
+ wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+
+ $output = get_echo( 'get_inline_data', array( get_post( self::$post_id ) ) );
+
+ $this->assertStringContainsString(
+ '0
',
+ $output
+ );
+ }
+}