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:
- 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():
$args = array(
'title' => $post_title,
'posts_per_page' => 2,
);
The taxonomy and author variants are not affected.
Change History (6)
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
## Use of AI Tools
Generated with Fable 5 and adjusted/reviewed manually.
#3
@
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 match → Templates 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
@
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!
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
I checked on trunk and it's replicable. With two published posts sharing a title:
2is the right value since the matching post is always in its own result set, and it matchesnumber => 2in 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.