Make WordPress Core

Changeset 61360


Ignore:
Timestamp:
12/08/2025 09:53:05 PM (5 days ago)
Author:
SergeyBiryukov
Message:

Posts, Post Types: Only set default title for custom post types if they have title support.

Previously, the default post title was always set to Auto Draft, regardless of whether the CPT supports a title.

This commit ensures that the default title is present when a CPT has title support, and is an empty string otherwise.

Follow-up to [12987], [49288], [49614].

Props SirLouen, rajanit2000, garrett-eclipse, wildworks, nrqsnchz, donmhico, marybaum, audrasjb, bridgetwillard, TimothyBlynJacobs, joyously, hellofromTonya, helen, Cybr, mosescursor, fakhriaz, gulamdastgir04, peterwilsoncc, mindctrl, westonruter, SergeyBiryukov.
Fixes #45516.

Location:
trunk
Files:
2 edited

Legend:

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

    r60701 r61360  
    765765        $post_id = wp_insert_post(
    766766            array(
    767                 'post_title'  => __( 'Auto Draft' ),
     767                'post_title'  => post_type_supports( $post_type, 'title' ) ? __( 'Auto Draft' ) : '',
    768768                'post_type'   => $post_type,
    769769                'post_status' => 'auto-draft',
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r60701 r61360  
    547547        $this->expectException( 'WPDieException' );
    548548        get_default_post_to_edit( 'post', true );
     549    }
     550
     551    /**
     552     * Tests that default post title is present when a CPT has title support, and is empty otherwise.
     553     *
     554     * @ticket 45516
     555     *
     556     * @covers ::get_default_post_to_edit
     557     */
     558    public function test_get_default_post_to_edit_with_and_without_title_support() {
     559        register_post_type(
     560            'yes_title',
     561            array(
     562                'supports' => array( 'title', 'editor' ),
     563            )
     564        );
     565        register_post_type(
     566            'no_title',
     567            array(
     568                'supports' => array( 'editor' ),
     569            )
     570        );
     571
     572        /*
     573         * The ID is obtained because get_default_post_to_edit() will force the post_title
     574         * to be overridden on the returned WP_Post object.
     575         */
     576        $default_yes_title_post_id = get_default_post_to_edit( 'yes_title', true )->ID;
     577        $default_no_title_post_id  = get_default_post_to_edit( 'no_title', true )->ID;
     578
     579        $this->assertSame( __( 'Auto Draft' ), get_post( $default_yes_title_post_id )->post_title, 'Expected post_title to be the default title.' );
     580        $this->assertSame( '', get_post( $default_no_title_post_id )->post_title, 'Expected post_title to be an empty string.' );
    549581    }
    550582
Note: See TracChangeset for help on using the changeset viewer.