diff --git a/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php b/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php index 656e38ffe933fa..0e16250acc89a2 100644 --- a/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php +++ b/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php @@ -113,7 +113,8 @@ public function get_template_fallback( $request ) { array_shift( $hierarchy ); } while ( ! empty( $hierarchy ) && empty( $fallback_template->content ) ); - $response = $this->prepare_item_for_response( $fallback_template, $request ); + // To maintain original behavior, return an empty object rather than a 404 error when no template is found. + $response = $fallback_template ? $this->prepare_item_for_response( $fallback_template, $request ) : new stdClass(); return rest_ensure_response( $response ); } diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 7de2c354ba2bf8..f040e548e61605 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -712,7 +712,8 @@ export const getDefaultTemplateId = const template = await apiFetch( { path: addQueryArgs( '/wp/v2/templates/lookup', query ), } ); - if ( template ) { + // Endpoint may return an empty object if no template is found. + if ( template?.id ) { dispatch.receiveDefaultTemplateId( query, template.id ); } }; diff --git a/packages/edit-post/src/store/selectors.js b/packages/edit-post/src/store/selectors.js index 787fc2f1d78d31..8510cbb501955e 100644 --- a/packages/edit-post/src/store/selectors.js +++ b/packages/edit-post/src/store/selectors.js @@ -609,10 +609,13 @@ export const getEditedPostTemplate = createRegistrySelector( const defaultTemplateId = select( coreStore ).getDefaultTemplateId( { slug: slugToCheck, } ); - return select( coreStore ).getEditedEntityRecord( - 'postType', - 'wp_template', - defaultTemplateId - ); + + return defaultTemplateId + ? select( coreStore ).getEditedEntityRecord( + 'postType', + 'wp_template', + defaultTemplateId + ) + : null; } );