Make WordPress Core

Changeset 35817


Ignore:
Timestamp:
12/07/2015 05:46:35 PM (9 years ago)
Author:
joehoyle
Message:

Prevent unnecessary $wpdb->update() when hierarchical post doesn't have children.

Props danielbachhuber.
Fixes #34896.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r35800 r35817  
    24062406        $children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type );
    24072407        $children = $wpdb->get_results( $children_query );
    2408 
    2409         $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) );
     2408        if ( $children ) {
     2409            $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) );
     2410        }
    24102411    }
    24112412
  • trunk/tests/phpunit/tests/post.php

    r35800 r35817  
    12541254        $this->assertEquals($post['post_date_gmt'], $out->post_date_gmt);
    12551255    }
     1256
     1257    function test_wp_delete_post_reassign_hierarchical_post_type() {
     1258        $grandparent_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
     1259        $parent_page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $grandparent_page_id ) );
     1260        $page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $parent_page_id ) );
     1261        $this->assertEquals( $parent_page_id, get_post( $page_id )->post_parent );
     1262        wp_delete_post( $parent_page_id, true );
     1263        $this->assertEquals( $grandparent_page_id, get_post( $page_id )->post_parent );
     1264        wp_delete_post( $grandparent_page_id, true );
     1265        $this->assertEquals( 0, get_post( $page_id )->post_parent );
     1266    }
     1267
    12561268}
Note: See TracChangeset for help on using the changeset viewer.