Make WordPress Core

Changeset 63349


Ignore:
Timestamp:
08/26/2026 01:29:04 PM (3 months ago)
Author:
ntsekouras
Message:

Editor: Append the post slug to single post template titles when posts share the same title.

When a template is created for a specific post or page, _wp_build_title_and_description_for_single_post_type_block_template() is supposed to append the post slug to the computed template title when more posts share the same title, so the templates can be distinguished in the Site Editor. However, the query checking for posts with the same title inherited posts_per_page => 1 from the default arguments, so it could never return more than one post and the check never matched. This has been the case since the function was introduced in WordPress 6.1.

The query now fetches up to two posts, matching the number => 2 argument already used by the taxonomy variant.

Props ntsekouras, mamaduka, jameskoster.
Fixes #65966.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r63320 r63349  
    730730
    731731        $args = array(
    732                 'title' => $post_title,
     732                'title'          => $post_title,
     733                'posts_per_page' => 2,
    733734        );
    734735        $args = wp_parse_args( $args, $default_args );
  • trunk/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php

    r58615 r63349  
    178178
    179179        /**
     180         * @ticket 65966
     181         */
     182        public function test_should_append_post_slug_to_title_when_posts_share_the_same_title() {
     183                self::factory()->post->create(
     184                        array(
     185                                'post_title' => 'Same Title',
     186                                'post_name'  => 'first-post',
     187                        )
     188                );
     189                self::factory()->post->create(
     190                        array(
     191                                'post_title' => 'Same Title',
     192                                'post_name'  => 'second-post',
     193                        )
     194                );
     195
     196                $first_template_post = self::factory()->post->create_and_get(
     197                        array(
     198                                'post_type'    => 'wp_template',
     199                                'post_name'    => 'single-post-first-post',
     200                                'post_title'   => 'single-post-first-post',
     201                                'post_content' => 'Content',
     202                                'post_excerpt' => '',
     203                                'tax_input'    => array(
     204                                        'wp_theme' => array(
     205                                                self::TEST_THEME,
     206                                        ),
     207                                ),
     208                        )
     209                );
     210                wp_set_post_terms( $first_template_post->ID, self::TEST_THEME, 'wp_theme' );
     211
     212                $second_template_post = self::factory()->post->create_and_get(
     213                        array(
     214                                'post_type'    => 'wp_template',
     215                                'post_name'    => 'single-post-second-post',
     216                                'post_title'   => 'single-post-second-post',
     217                                'post_content' => 'Content',
     218                                'post_excerpt' => '',
     219                                'tax_input'    => array(
     220                                        'wp_theme' => array(
     221                                                self::TEST_THEME,
     222                                        ),
     223                                ),
     224                        )
     225                );
     226                wp_set_post_terms( $second_template_post->ID, self::TEST_THEME, 'wp_theme' );
     227
     228                $first_template  = _build_block_template_result_from_post( $first_template_post );
     229                $second_template = _build_block_template_result_from_post( $second_template_post );
     230
     231                $this->assertNotWPError( $first_template, 'Building the template for the first post should not return an error.' );
     232                $this->assertSame( 'Post: Same Title (first-post)', $first_template->title, 'The title of the first post template should be suffixed with the post slug.' );
     233                $this->assertNotWPError( $second_template, 'Building the template for the second post should not return an error.' );
     234                $this->assertSame( 'Post: Same Title (second-post)', $second_template->title, 'The title of the second post template should be suffixed with the post slug.' );
     235        }
     236
     237        /**
     238         * @ticket 65966
     239         */
     240        public function test_should_not_append_post_slug_to_title_when_post_title_is_unique() {
     241                self::factory()->post->create(
     242                        array(
     243                                'post_title' => 'Unique Title',
     244                                'post_name'  => 'unique-post',
     245                        )
     246                );
     247
     248                $template_post = self::factory()->post->create_and_get(
     249                        array(
     250                                'post_type'    => 'wp_template',
     251                                'post_name'    => 'single-post-unique-post',
     252                                'post_title'   => 'single-post-unique-post',
     253                                'post_content' => 'Content',
     254                                'post_excerpt' => '',
     255                                'tax_input'    => array(
     256                                        'wp_theme' => array(
     257                                                self::TEST_THEME,
     258                                        ),
     259                                ),
     260                        )
     261                );
     262                wp_set_post_terms( $template_post->ID, self::TEST_THEME, 'wp_theme' );
     263
     264                $template = _build_block_template_result_from_post( $template_post );
     265
     266                $this->assertNotWPError( $template, 'Building the template should not return an error.' );
     267                $this->assertSame( 'Post: Unique Title', $template->title, 'The template title should not be suffixed with the post slug when the post title is unique.' );
     268        }
     269
     270        /**
     271         * @ticket 65966
     272         */
     273        public function test_should_append_term_slug_to_title_when_terms_share_the_same_name() {
     274                self::factory()->term->create(
     275                        array(
     276                                'taxonomy' => 'post_tag',
     277                                'name'     => 'Same Name',
     278                                'slug'     => 'first-tag',
     279                        )
     280                );
     281                self::factory()->term->create(
     282                        array(
     283                                'taxonomy' => 'post_tag',
     284                                'name'     => 'Same Name',
     285                                'slug'     => 'second-tag',
     286                        )
     287                );
     288
     289                $first_template_post = self::factory()->post->create_and_get(
     290                        array(
     291                                'post_type'    => 'wp_template',
     292                                'post_name'    => 'tag-first-tag',
     293                                'post_title'   => 'tag-first-tag',
     294                                'post_content' => 'Content',
     295                                'post_excerpt' => '',
     296                                'tax_input'    => array(
     297                                        'wp_theme' => array(
     298                                                self::TEST_THEME,
     299                                        ),
     300                                ),
     301                        )
     302                );
     303                wp_set_post_terms( $first_template_post->ID, self::TEST_THEME, 'wp_theme' );
     304
     305                $second_template_post = self::factory()->post->create_and_get(
     306                        array(
     307                                'post_type'    => 'wp_template',
     308                                'post_name'    => 'tag-second-tag',
     309                                'post_title'   => 'tag-second-tag',
     310                                'post_content' => 'Content',
     311                                'post_excerpt' => '',
     312                                'tax_input'    => array(
     313                                        'wp_theme' => array(
     314                                                self::TEST_THEME,
     315                                        ),
     316                                ),
     317                        )
     318                );
     319                wp_set_post_terms( $second_template_post->ID, self::TEST_THEME, 'wp_theme' );
     320
     321                $first_template  = _build_block_template_result_from_post( $first_template_post );
     322                $second_template = _build_block_template_result_from_post( $second_template_post );
     323
     324                $this->assertNotWPError( $first_template, 'Building the template for the first term should not return an error.' );
     325                $this->assertSame( 'Tag: Same Name (first-tag)', $first_template->title, 'The title of the first term template should be suffixed with the term slug.' );
     326                $this->assertNotWPError( $second_template, 'Building the template for the second term should not return an error.' );
     327                $this->assertSame( 'Tag: Same Name (second-tag)', $second_template->title, 'The title of the second term template should be suffixed with the term slug.' );
     328        }
     329
     330        /**
     331         * @ticket 65966
     332         */
     333        public function test_should_not_append_term_slug_to_title_when_term_name_is_unique() {
     334                self::factory()->term->create(
     335                        array(
     336                                'taxonomy' => 'category',
     337                                'name'     => 'Unique Category',
     338                                'slug'     => 'unique-category',
     339                        )
     340                );
     341
     342                $template_post = self::factory()->post->create_and_get(
     343                        array(
     344                                'post_type'    => 'wp_template',
     345                                'post_name'    => 'category-unique-category',
     346                                'post_title'   => 'category-unique-category',
     347                                'post_content' => 'Content',
     348                                'post_excerpt' => '',
     349                                'tax_input'    => array(
     350                                        'wp_theme' => array(
     351                                                self::TEST_THEME,
     352                                        ),
     353                                ),
     354                        )
     355                );
     356                wp_set_post_terms( $template_post->ID, self::TEST_THEME, 'wp_theme' );
     357
     358                $template = _build_block_template_result_from_post( $template_post );
     359
     360                $this->assertNotWPError( $template, 'Building the template should not return an error.' );
     361                $this->assertSame( 'Category: Unique Category', $template->title, 'The template title should not be suffixed with the term slug when the term name is unique.' );
     362        }
     363
     364        /**
    180365         * @ticket 59646
    181366         * @ticket 60506
Note: See TracChangeset for help on using the changeset viewer.