Make WordPress Core

Ticket #59354: 59354.diff

File 59354.diff, 2.1 KB (added by motylanogha, 6 months ago)

Patch for current trunk. Skips wp_set_post_categories() on update when post_category is not in $postarr, matching the existing tags_input guard. Includes unit test verifying fewer queries. All 62 wp_insert_post tests pass.

  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index b225d35c48..e51df957b5 100644
    a b function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true )  
    49974997                clean_post_cache( $post_id );
    49984998        }
    49994999
    5000         if ( is_object_in_taxonomy( $post_type, 'category' ) ) {
     5000        if ( is_object_in_taxonomy( $post_type, 'category' ) && ( ! $update || isset( $postarr['post_category'] ) ) ) {
    50015001                wp_set_post_categories( $post_id, $post_category );
    50025002        }
    50035003
  • tests/phpunit/tests/post/wpInsertPost.php

    diff --git a/tests/phpunit/tests/post/wpInsertPost.php b/tests/phpunit/tests/post/wpInsertPost.php
    index 1227984834..fd36f6b11a 100644
    a b class Tests_Post_wpInsertPost extends WP_UnitTestCase {  
    15301530
    15311531                $this->assertSame( 'future', get_post_status( $post_id ) );
    15321532        }
     1533
     1534        /**
     1535         * Verify that updating a post without taxonomy parameters avoids unnecessary queries.
     1536         *
     1537         * @ticket 59354
     1538         * @covers ::wp_insert_post
     1539         */
     1540        public function test_update_post_without_taxonomy_params_skips_term_queries() {
     1541                $post_id = self::factory()->post->create(
     1542                        array(
     1543                                'post_title'    => 'Test Post',
     1544                                'tags_input'    => array( 'tag1' ),
     1545                                'post_category' => array( 1 ),
     1546                        )
     1547                );
     1548
     1549                // Update with taxonomy params — baseline query count.
     1550                $queries_before = get_num_queries();
     1551                wp_insert_post(
     1552                        array(
     1553                                'ID'            => $post_id,
     1554                                'post_title'    => 'Updated With Taxes',
     1555                                'tags_input'    => array( 'tag1' ),
     1556                                'post_category' => array( 1 ),
     1557                        )
     1558                );
     1559                $queries_with_tax = get_num_queries() - $queries_before;
     1560
     1561                // Update without taxonomy params — should use fewer queries.
     1562                $queries_before = get_num_queries();
     1563                wp_insert_post(
     1564                        array(
     1565                                'ID'         => $post_id,
     1566                                'post_title' => 'Updated Without Taxes',
     1567                        )
     1568                );
     1569                $queries_without_tax = get_num_queries() - $queries_before;
     1570
     1571                $this->assertLessThan(
     1572                        $queries_with_tax,
     1573                        $queries_without_tax,
     1574                        'Updating a post without taxonomy parameters should use fewer queries.'
     1575                );
     1576        }
    15331577}