Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cleaned slug to query for template part post #25030

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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