Skip to content

Commit b1c8026

Browse files
authored
Use cleaned slug to query for template part post (#25030)
Fixes issues where theme template parts were not resolved from subdirectories.
1 parent a3cd345 commit b1c8026

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

lib/template-loader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function create_auto_draft_for_template_part_block( $block ) {
148148
array(
149149
'post_type' => 'wp_template_part',
150150
'post_status' => array( 'publish', 'auto-draft' ),
151-
'name' => $block['attrs']['slug'],
151+
'title' => $block['attrs']['slug'],
152152
'meta_key' => 'theme',
153153
'meta_value' => $block['attrs']['theme'],
154154
'posts_per_page' => 1,

packages/block-library/src/template-part/edit/use-template-part-post.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* WordPress dependencies
33
*/
44
import { useSelect } from '@wordpress/data';
5+
import { cleanForSlug } from '@wordpress/url';
56

67
export default function useTemplatePartPost( postId, slug, theme ) {
78
return useSelect(
@@ -22,18 +23,19 @@ export default function useTemplatePartPost( postId, slug, theme ) {
2223
// load the auto-draft created from the
2324
// relevant file.
2425
if ( slug && theme ) {
26+
const cleanedSlug = cleanForSlug( slug );
2527
const posts = select( 'core' ).getEntityRecords(
2628
'postType',
2729
'wp_template_part',
2830
{
2931
status: [ 'publish', 'auto-draft' ],
30-
slug,
32+
slug: cleanedSlug,
3133
theme,
3234
}
3335
);
3436
const foundPosts = posts?.filter(
3537
( post ) =>
36-
post.slug === slug &&
38+
post.slug === cleanedSlug &&
3739
post.meta &&
3840
post.meta.theme === theme
3941
);

0 commit comments

Comments
 (0)