diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 7a1e2a81b4..0e19876e51 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -610,9 +610,11 @@ function bulk_edit_posts( $post_data = null ) { if ( isset( $shared_post_data['post_format'] ) ) { set_post_format( $post_ID, $shared_post_data['post_format'] ); - unset( $post_data['tax_input']['post_format'] ); } + // Prevent wp_insert_post() from overwriting post format with the old data. + unset( $post_data['tax_input']['post_format'] ); + $updated[] = wp_update_post( $post_data ); if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) { diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index ee3edb8aa0..76802b4fa1 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -254,6 +254,41 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase { $this->assertEquals( 'closed', $post->ping_status ); } + /** + * The bulk_edit_posts() function should preserve the post format + * when it's unchanged. + * + * @ticket 44914 + */ + public function test_bulk_edit_posts_should_preserve_post_format_when_unchanged() { + wp_set_current_user( self::$admin_id ); + + $post_ids = self::factory()->post->create_many( 3 ); + + set_post_format( $post_ids[0], 'image' ); + set_post_format( $post_ids[1], 'aside' ); + + $request = array( + 'post_format' => -1, // Don't change the post format. + '_status' => -1, + 'post' => $post_ids, + ); + + bulk_edit_posts( $request ); + + $terms1 = get_the_terms( $post_ids[0], 'post_format' ); + $terms2 = get_the_terms( $post_ids[1], 'post_format' ); + $terms3 = get_the_terms( $post_ids[2], 'post_format' ); + + $this->assertSame( 'post-format-image', $terms1[0]->slug ); + $this->assertSame( 'post-format-aside', $terms2[0]->slug ); + $this->assertFalse( $terms3 ); + + $this->assertSame( 'image', get_post_format( $post_ids[0] ) ); + $this->assertSame( 'aside', get_post_format( $post_ids[1] ) ); + $this->assertFalse( get_post_format( $post_ids[2] ) ); + } + /** * @ticket 41396 */