diff --git a/packages/block-library/src/query-loop/edit.js b/packages/block-library/src/query-loop/edit.js index a32ffd6741050..4d62b24356bb5 100644 --- a/packages/block-library/src/query-loop/edit.js +++ b/packages/block-library/src/query-loop/edit.js @@ -41,6 +41,7 @@ export default function QueryLoopEdit( { search, exclude, sticky, + inherit, } = {}, queryContext, layout: { type: layoutType = 'flex', columns = 1 } = {}, @@ -78,6 +79,30 @@ export default function QueryLoopEdit( { if ( sticky ) { query.sticky = sticky === 'only'; } + + // When you insert this block outside of the edit site then store + // does not exist therefore we check for its existence. + if ( inherit && select( 'core/edit-site' ) ) { + // This should be passed from the context exposed by edit site. + const { getTemplateId, getTemplateType } = select( + 'core/edit-site' + ); + + if ( 'wp_template' === getTemplateType() ) { + const { slug } = select( 'core' ).getEntityRecord( + 'postType', + 'wp_template', + getTemplateId() + ); + + // Change the post-type if needed. + if ( slug?.startsWith( 'archive-' ) ) { + query.postType = slug.replace( 'archive-', '' ); + postType = query.postType; + } + } + } + return { posts: getEntityRecords( 'postType', postType, query ), blocks: getBlocks( clientId ), @@ -97,6 +122,7 @@ export default function QueryLoopEdit( { postType, exclude, sticky, + inherit, ] ); diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index d1aef57b1cb66..f611e4fbd81d7 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -42,7 +42,8 @@ function render_block_core_query_loop( $attributes, $content, $block ) { $query['post__not_in'] = array_merge( $query['post__not_in'], $block->context['query']['exclude'] ); } if ( isset( $block->context['query']['perPage'] ) ) { - $query['offset'] = ( $block->context['query']['perPage'] * ( $page - 1 ) ) + $block->context['query']['offset']; + $query['offset'] = ( $block->context['query']['perPage'] * ( $page - 1 ) ) + $block->context['query']['offset']; + $query['posts_per_page'] = $block->context['query']['perPage']; } if ( isset( $block->context['query']['categoryIds'] ) ) { $query['category__in'] = $block->context['query']['categoryIds']; @@ -56,9 +57,6 @@ function render_block_core_query_loop( $attributes, $content, $block ) { if ( isset( $block->context['query']['orderBy'] ) ) { $query['orderby'] = $block->context['query']['orderBy']; } - if ( isset( $block->context['query']['perPage'] ) ) { - $query['posts_per_page'] = $block->context['query']['perPage']; - } if ( isset( $block->context['query']['author'] ) ) { $query['author'] = $block->context['query']['author']; } @@ -67,6 +65,15 @@ function render_block_core_query_loop( $attributes, $content, $block ) { } } + // Override the custom query with the global query if needed. + $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); + if ( $use_global_query ) { + global $wp_query; + if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { + $query = wp_parse_args( $wp_query->query_vars, $query ); + } + } + $posts = get_posts( $query ); $classnames = ''; @@ -89,7 +96,7 @@ function render_block_core_query_loop( $attributes, $content, $block ) { ) ) )->render( array( 'dynamic' => false ) ); - $content .= "