Template field: Fix default label and skip wasted front-page lookup#78338
Template field: Fix default label and skip wasted front-page lookup#78338ellatrix wants to merge 1 commit into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Size Change: -156 B (0%) Total Size: 7.96 MB 📦 View Changed
ℹ️ View Unchanged
|
|
Flaky tests detected in 7f8df18. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/25915215214
|
`getTemplateId` called `getHomePage()` (and `getPostsPageId()`)
unconditionally before checking `postType === 'page'`.
`getHomePage()` internally calls `getDefaultTemplateId({ slug: 'front-page' })`,
which fires `GET /wp/v2/templates/lookup?slug=front-page` — wasted on every
non-page post type since the result is discarded by the subsequent
post-type guard.
Move the `postType === 'page'` check ahead of both resolver calls. Matches
the short-circuit pattern already used in `template-edit.tsx`
(`data.type === 'page' && getHomePage()`).
Also: `useDefaultTemplateLabel` was open-coding the same homepage /
posts-page / template-hierarchy resolution as `getTemplateId`, slightly
differently — its default lookup used `post` as the slug for non-page
post types, which isn't part of WordPress's `single-{post-type}` template
hierarchy, so it would frequently return nothing. Replace the duplicated
body with a `getTemplateId` call and look up the resolved template's
title.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
7f8df18 to
74a99d2
Compare
| return NAME_NOT_FOUND; | ||
| } | ||
|
|
||
| const postIdStr = String( postId ); |
There was a problem hiding this comment.
The main difference between the hook and the selector is that the hook would always return the default fallback template and not the assigned one. In this PR you can see that this leads to the below:
Screen.Recording.2026-05-15.at.5.19.42.PM.mov
What do you mean here? I don't think that was ever the case with
In general a theme could only have an |
| // branches below are guarded by `postType === 'page'`, so resolve | ||
| // them only when the post is actually a page — otherwise the | ||
| // lookup is wasted work for every non-page post type. | ||
| if ( postType === 'page' ) { |
There was a problem hiding this comment.
Changes in this file could be split in a separate PR.
The post-editor's templates/lookup currently fires with `slug=front-page` on trunk (the unconditional `getHomePage()` call in `getTemplateId`). My local was running with #78338's fix applied which changes this to `slug=single-post`, but trunk's behaviour is still `front-page`. Match trunk for now; #78338 will flip this to `single-post`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
Best reviewed without whitespace.
What & Why
This PR does three things:
Skips a wasted REST request on non-page posts.
getTemplateIdcalledgetHomePage()/getPostsPageId()before itspostType === 'page'guard.getHomePage()firesGET /wp/v2/templates/lookup?slug=front-pageviagetDefaultTemplateId. The result was then discarded for every non-page post. The post-editor preload e2e spec surfaced this.Removes duplicated logic.
useDefaultTemplateLabel(in@wordpress/fields) re-implemented the same homepage / posts-page / template-hierarchy resolution asgetTemplateId(in@wordpress/core-data). Two places to keep in sync as the resolution rules evolve.Fixes the wrong-template-label bug. The hook used a bare post-type name as the lookup slug for non-page post types (
post,product, …). WordPress's hierarchy issingle-{post-type}-{slug}→single-{post-type}→single→singular→index, with no bare{post-type}in it. Empirically,/wp/v2/templates/lookup?slug=postfalls all the way through totwentytwentyfive//index("Index") — wrong, that's the listing template.slug=single-postcorrectly resolves tosingle.html("Single Posts"). So the Template field's default label was misleading for any non-page post.How
postType === 'page'guard beforegetHomePage()andgetPostsPageId()ingetTemplateId.useDefaultTemplateLabel's body with agetTemplateIdcall, removing the localgetTemplateSlugToCheckhelper and the now-unusedslugparameter.History
useDefaultTemplateLabel. Already referencedgetTemplateIdin a comment, so the duplication was known from the start.template#76441 (Mar 2026, @ntsekouras) — added classic-themes branching to the same hook.Testing Instructions
templates/lookup, confirm noslug=front-pagerequest fires.Use of AI Tools
Authored by Claude (Opus 4.7) in collaboration with @ellatrix.