From 7c7c2062934000bdb77d668a29e99767102f9f93 Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Mon, 4 Nov 2024 11:49:49 +0100 Subject: [PATCH] Post template block: Adjust the editor preview on taxonomy archive templates Ensure that the preview of the post template block in the editor shows the correct posts on tag- and post format archive templates, when the query loop block is set to inherit the query from the template. Co-authored-by: carolinan Co-authored-by: ntsekouras --- .../block-library/src/post-template/edit.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/block-library/src/post-template/edit.js b/packages/block-library/src/post-template/edit.js index a9e279ee2c305..c58990233f361 100644 --- a/packages/block-library/src/post-template/edit.js +++ b/packages/block-library/src/post-template/edit.js @@ -119,6 +119,15 @@ export default function PostTemplateEdit( { _fields: [ 'id' ], slug: templateSlug.replace( 'category-', '' ), } ); + const templateTag = + inherit && + templateSlug?.startsWith( 'tag-' ) && + getEntityRecords( 'taxonomy', 'post_tag', { + context: 'view', + per_page: 1, + _fields: [ 'id' ], + slug: templateSlug.replace( 'tag-', '' ), + } ); const query = { offset: offset || 0, order, @@ -182,6 +191,16 @@ export default function PostTemplateEdit( { postType = query.postType; } else if ( templateCategory ) { query.categories = templateCategory[ 0 ]?.id; + } else if ( templateTag ) { + query.tags = templateTag[ 0 ]?.id; + } else if ( + templateSlug?.startsWith( 'taxonomy-post_format' ) + ) { + // Get the post format slug from the template slug by removing the prefix. + query.format = templateSlug.replace( + 'taxonomy-post_format-post-format-', + '' + ); } } // When we preview Query Loop blocks we should prefer the current