Make WordPress Core

Opened 25 hours ago

Closed 21 hours ago

#65966 closed defect (bug) (fixed)

Templates for posts with the same title never get the slug suffix appended

Reported by: ntsekouras Owned by: ntsekouras
Priority: normal Milestone: Awaiting Review
Component: General Version:
Severity: normal Keywords:
Cc: Focuses:

Description

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 they can be distinguished in the templates list.

That check can never match though. The second query only sets title and is merged into $default_args, which contains posts_per_page => 1, so count( $posts_with_same_title_query->posts ) > 1 is always false. This has been the case since the function was introduced in 6.1(https://github.com/WordPress/gutenberg/pull/43862).

Steps to reproduce:

  1. Create two published posts with the same title.
  2. In the Site Editor, create a template for each of them (Templates > Add template > Single item: Post).
  3. Both templates show the same title (Post: {title}), without the (slug) suffix.

The fix is to let the second query fetch up to two posts, same as the taxonomy variant already does with number => 2 in _wp_build_title_and_description_for_taxonomy_block_template():

$args = array(
      'title'          => $post_title,
      'posts_per_page' => 2,
);

The taxonomy and author variants are not affected.

Change History (6)

#1 @iamchitti
24 hours ago

I checked on trunk and it's replicable. With two published posts sharing a title:

Before:                title = Post: Shared Title Repro   (both slugs)
posts_per_page => 2:   title = Post: Shared Title Repro (shared-title-repro-a)

2 is the right value since the matching post is always in its own result set, and it matches number => 2 in the taxonomy variant. A unique title, a draft-only sibling, and a missing post are all unaffected.

Neither this nor the taxonomy variant has test coverage, which is likely why it went unnoticed since 6.1. Happy to put up a PR with the fix plus tests, or just tests if you'd rather take the fix — let me know.

This ticket was mentioned in PR #13278 on WordPress/wordpress-develop by @ntsekouras.


24 hours ago
#2

  • Keywords has-patch has-unit-tests added

Trac ticket: https://core.trac.wordpress.org/ticket/65966

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 they can be distinguished in the templates list.

That check can never match though. The second query only sets title and is merged into $default_args, which contains posts_per_page => 1, so count( $posts_with_same_title_query->posts ) > 1 is always false. This has been the case since the function was introduced in 6.1(https://github.com/WordPress/gutenberg/pull/43862).

### Steps to reproduce:

Create two published posts with the same title.
In the Site Editor, create a template for each of them (Templates > Add template > Single item: Post).
Both templates show the same title (Post: {title}), without the (slug) suffix.
The fix is to let the second query fetch up to two posts, same as the taxonomy variant already does with number => 2 in _wp_build_title_and_description_for_taxonomy_block_template():

{{{PHP
$args = array(

'title' => $post_title,
'posts_per_page' => 2,

);
}}}
The taxonomy and author variants are not affected.

## Screenshots

https://github.com/user-attachments/assets/3e6f12e2-53b8-4143-99d1-af5584cf4f77

## Use of AI Tools
Generated with Fable 5 and adjusted/reviewed manually.

#3 @ntsekouras
24 hours ago

  • Keywords has-patch has-unit-tests removed
  • Summary Same title check in `_wp_build_title_and_description_for_single_post_type_block_template` can never matchTemplates for posts with the same title never get the slug suffix appended

Happy to put up a PR with the fix plus tests, or just tests if you'd rather take the fix — let me know.

Thanks @iamchitti! I already opened the PR. It was very small, thus I had assigned myself during the creation of the ticket.

#4 @sainathpoojary
24 hours ago

Thanks for the fix, @ntsekouras! I've tested the patch and it works perfectly. The fix is spot on and the new tests are great.

As @iamchitti pointed out earlier, the taxonomy variant (_wp_build_title_and_description_for_taxonomy_block_template) is also missing test coverage. I've confirmed there are indeed no tests for it currently.

Do you think it makes sense to include tests for the taxonomy variant in this PR, or should that be handled separately in a new ticket/PR?

Let me know what you think!

#5 @ntsekouras
23 hours ago

Do you think it makes sense to include tests for the taxonomy variant in this PR, or should that be handled separately in a new ticket/PR?

I guess it's fine to add a couple more tests for taxonomies in the same PR. I'll add them shortly.

#6 @ntsekouras
21 hours ago

  • Resolutionfixed
  • Status assignedclosed

In 63349:

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.

Note: See TracTickets for help on using tickets.