From 60c51daa81da644e31047a954a38463beeb7226d Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:50:52 +0300 Subject: [PATCH 1/2] Quick/Bulk Edit: Reset category states each time Bulk Edit opens. `inlineEditPost.setBulk()` set category checkboxes to checked or indeterminate but never cleared them. Reopening Bulk Edit without a page reload kept categories checked from the previous selection, so saving assigned those categories (typically "Uncategorized") to posts that were not in them. Category states and the hidden `indeterminate_post_category[]` inputs are now reset before the new states are computed. The checkbox selectors are scoped to the Bulk Edit row so they no longer change the Quick Edit form, and the change and click handlers are namespaced so they are not bound again on every open. Props abditolessa. Co-Authored-By: Claude Opus 5.5 --- src/js/_enqueues/admin/inline-edit-post.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/js/_enqueues/admin/inline-edit-post.js b/src/js/_enqueues/admin/inline-edit-post.js index aaf4bcc5bc3ec..4f9ee74705531 100644 --- a/src/js/_enqueues/admin/inline-edit-post.js +++ b/src/js/_enqueues/admin/inline-edit-post.js @@ -249,8 +249,21 @@ window.wp = window.wp || {}; } ); } ); + var $bulkCategories = $( '#bulk-edit .inline-edit-categories input[name="post_category[]"]' ); + + /* + * Reset category states left over from a previous Bulk Edit session. + * Otherwise a category checked for a previous selection stays checked + * and gets assigned to posts that are not in it. + */ + $bulkCategories + .prop( { checked: false, indeterminate: false } ) + .removeAttr( 'aria-label' ) + .off( 'change.inlineEditPost' ); + $( '#bulk-edit .inline-edit-categories input[name="indeterminate_post_category[]"]' ).remove(); + // Compute initial states. - $( '.inline-edit-categories input[name="post_category[]"]' ).each( function() { + $bulkCategories.each( function() { if ( categories[ $( this ).val() ] == checkedPosts.length ) { // If the number of checked categories matches the number of selected posts, then all posts are in this category. $( this ).prop( 'checked', true ); @@ -266,13 +279,13 @@ window.wp = window.wp || {}; } } ); - $( '.inline-edit-categories input[name="post_category[]"]:indeterminate' ).on( 'change', function() { + $bulkCategories.filter( ':indeterminate' ).on( 'change.inlineEditPost', function() { // Remove accessible label text. Remove the indeterminate flags as there was a specific state change. $( this ).removeAttr( 'aria-label' ).parent().find( 'input[name="indeterminate_post_category[]"]' ).remove(); } ); - $( '.inline-edit-save button' ).on( 'click', function() { - $( '.inline-edit-categories input[name="post_category[]"]' ).prop( 'indeterminate', false ); + $( '#bulk-edit .inline-edit-save button' ).off( 'click.inlineEditPost' ).on( 'click.inlineEditPost', function() { + $bulkCategories.prop( 'indeterminate', false ); } ); /** From dcf6c8eca83feec723e28de5e8c23467ba9ba8e4 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:58:00 +0300 Subject: [PATCH 2/2] Quick/Bulk Edit: Allow removing all categories from posts in Bulk Edit. When the selected posts are only in the default category and it is unchecked in Bulk Edit, `wp_insert_post()` assigned the default category again, so it could not be removed. `bulk_edit_posts()` now removes the categories of a post whose category list ends up empty, provided the user can assign categories. Props abditolessa. Co-Authored-By: Claude Opus 5.5 --- src/wp-admin/includes/post.php | 16 +++- tests/phpunit/tests/admin/includesPost.php | 86 ++++++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index f08f81766df67..6885c1913b827 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -639,8 +639,9 @@ function bulk_edit_posts( $post_data = null ) { continue; } - $post = get_post( $post_id ); - $tax_names = get_object_taxonomies( $post ); + $post = get_post( $post_id ); + $tax_names = get_object_taxonomies( $post ); + $remove_all_categories = false; foreach ( $tax_names as $tax_name ) { $taxonomy_obj = get_taxonomy( $tax_name ); @@ -680,6 +681,13 @@ function bulk_edit_posts( $post_data = null ) { $determinate_cats = array_diff( $new_cats, $indeterminate_post_category ); $post_data['post_category'] = array_unique( array_merge( $indeterminate_cats, $determinate_cats ) ); + /* + * Every category was unchecked for this post. wp_insert_post() would assign + * the default category, so remove the categories after the post is updated. + */ + $remove_all_categories = ! array_filter( array_map( 'absint', $post_data['post_category'] ) ) + && current_user_can( get_taxonomy( 'category' )->cap->assign_terms ); + unset( $post_data['tax_input']['category'] ); } @@ -720,6 +728,10 @@ function bulk_edit_posts( $post_data = null ) { update_post_meta( $post_id, '_edit_last', get_current_user_id() ); $updated[] = $post_id; + if ( $remove_all_categories && $post_id ) { + wp_set_object_terms( $post_id, array(), 'category' ); + } + if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) { if ( 'sticky' === $post_data['sticky'] ) { stick_post( $post_id ); diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index fc4ba33776de5..888f36fbdf239 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -509,6 +509,92 @@ public function test_bulk_edit_if_some_categories_removed() { $this->assertSame( $updated_terms3, array( $term3 ), 'Post 3 should only have term 3.' ); } + /** + * Tests that unchecking the default category in Bulk Edit removes it from posts + * that have no other category, rather than reassigning it. + * + * @ticket 59374 + * + * @covers ::bulk_edit_posts + */ + public function test_bulk_edit_removing_default_category_from_posts_without_other_categories() { + wp_set_current_user( self::$admin_id ); + + $default_category = (int) get_option( 'default_category' ); + $post_ids = self::factory()->post->create_many( 2 ); + + $this->assertSame( array( $default_category ), wp_get_post_categories( $post_ids[0] ), 'Post 1 should start in the default category.' ); + $this->assertSame( array( $default_category ), wp_get_post_categories( $post_ids[1] ), 'Post 2 should start in the default category.' ); + + // The Bulk Edit category checklist always submits a hidden value of 0. + $request = array( + '_status' => -1, + 'post' => $post_ids, + 'post_category' => array( '0' ), + ); + + bulk_edit_posts( $request ); + + $this->assertSame( array(), wp_get_post_categories( $post_ids[0] ), 'Post 1 should have no categories.' ); + $this->assertSame( array(), wp_get_post_categories( $post_ids[1] ), 'Post 2 should have no categories.' ); + } + + /** + * Tests that unchecking the default category in Bulk Edit keeps the other + * (indeterminate) categories and removes the default category from all posts. + * + * @ticket 59374 + * + * @covers ::bulk_edit_posts + */ + public function test_bulk_edit_removing_default_category_from_posts_with_mixed_categories() { + wp_set_current_user( self::$admin_id ); + + $default_category = (int) get_option( 'default_category' ); + $term1 = wp_create_category( 'test1' ); + $post_ids = self::factory()->post->create_many( 2 ); + + wp_set_post_categories( $post_ids[1], array( $default_category, $term1 ) ); + + // The default category is checked for all posts, term 1 is indeterminate. + $request = array( + '_status' => -1, + 'post' => $post_ids, + 'post_category' => array( '0' ), + 'indeterminate_post_category' => array( (string) $term1 ), + ); + + bulk_edit_posts( $request ); + + $this->assertSame( array(), wp_get_post_categories( $post_ids[0] ), 'Post 1 should have no categories.' ); + $this->assertSame( array( $term1 ), wp_get_post_categories( $post_ids[1] ), 'Post 2 should only have term 1.' ); + } + + /** + * Tests that Bulk Edit keeps the default category when it stays checked. + * + * @ticket 59374 + * + * @covers ::bulk_edit_posts + */ + public function test_bulk_edit_keeps_default_category_when_left_checked() { + wp_set_current_user( self::$admin_id ); + + $default_category = (int) get_option( 'default_category' ); + $post_ids = self::factory()->post->create_many( 2 ); + + $request = array( + '_status' => -1, + 'post' => $post_ids, + 'post_category' => array( '0', (string) $default_category ), + ); + + bulk_edit_posts( $request ); + + $this->assertSame( array( $default_category ), wp_get_post_categories( $post_ids[0] ), 'Post 1 should keep the default category.' ); + $this->assertSame( array( $default_category ), wp_get_post_categories( $post_ids[1] ), 'Post 2 should keep the default category.' ); + } + /** * Tests that `bulk_edit_posts()` fires the 'bulk_edit_posts' action. *