Skip to content

Commit

Permalink
Use cleaned slug to query for template part post (#25030)
Browse files Browse the repository at this point in the history
Fixes issues where theme template parts
were not resolved from subdirectories.
  • Loading branch information
noahtallen authored Sep 3, 2020
1 parent a3cd345 commit b1c8026
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function create_auto_draft_for_template_part_block( $block ) {
array(
'post_type' => 'wp_template_part',
'post_status' => array( 'publish', 'auto-draft' ),
'name' => $block['attrs']['slug'],
'title' => $block['attrs']['slug'],
'meta_key' => 'theme',
'meta_value' => $block['attrs']['theme'],
'posts_per_page' => 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { cleanForSlug } from '@wordpress/url';

export default function useTemplatePartPost( postId, slug, theme ) {
return useSelect(
Expand All @@ -22,18 +23,19 @@ export default function useTemplatePartPost( postId, slug, theme ) {
// load the auto-draft created from the
// relevant file.
if ( slug && theme ) {
const cleanedSlug = cleanForSlug( slug );
const posts = select( 'core' ).getEntityRecords(
'postType',
'wp_template_part',
{
status: [ 'publish', 'auto-draft' ],
slug,
slug: cleanedSlug,
theme,
}
);
const foundPosts = posts?.filter(
( post ) =>
post.slug === slug &&
post.slug === cleanedSlug &&
post.meta &&
post.meta.theme === theme
);
Expand Down

0 comments on commit b1c8026

Please sign in to comment.