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 .= "
  • {$block_content}
  • "; + $content .= "
  • {$block_content}
  • "; } return sprintf( '', diff --git a/packages/block-library/src/query/block.json b/packages/block-library/src/query/block.json index 7477e03d6f848..016fa44fbcc0e 100644 --- a/packages/block-library/src/query/block.json +++ b/packages/block-library/src/query/block.json @@ -20,7 +20,8 @@ "author": "", "search": "", "exclude": [], - "sticky": "" + "sticky": "", + "inherit": true } }, "layout": { diff --git a/packages/block-library/src/query/edit/query-inspector-controls.js b/packages/block-library/src/query/edit/query-inspector-controls.js index 564a37348a2c8..0615908057582 100644 --- a/packages/block-library/src/query/edit/query-inspector-controls.js +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -14,6 +14,7 @@ import { FormTokenField, SelectControl, RangeControl, + ToggleControl, Notice, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; @@ -44,6 +45,7 @@ export default function QueryInspectorControls( { author: selectedAuthorId, postType, sticky, + inherit, } = query; const [ showCategories, setShowCategories ] = useState( true ); const [ showTags, setShowTags ] = useState( true ); @@ -136,6 +138,7 @@ export default function QueryInspectorControls( { }, 250 ), [ querySearch, query.search ] ); + useEffect( () => { onChangeDebounced(); return onChangeDebounced.cancel; @@ -143,12 +146,22 @@ export default function QueryInspectorControls( { return ( - setQuery( { inherit: !! value } ) } /> + { ! inherit && ( + + ) } { layout?.type === 'flex' && ( <> ) } - setQuery( { order: value } ) } - onOrderByChange={ ( value ) => - setQuery( { orderBy: value } ) - } - /> + { ! inherit && ( + + setQuery( { order: value } ) + } + onOrderByChange={ ( value ) => + setQuery( { orderBy: value } ) + } + /> + ) } { showSticky && ( ) } - - { showCategories && categories?.terms?.length > 0 && ( - ( { - id: categoryId, - value: categories.mapById[ categoryId ].name, + { ! inherit && ( + + { showCategories && categories?.terms?.length > 0 && ( + ( { + id: categoryId, + value: + categories.mapById[ categoryId ].name, + } ) + ) } + suggestions={ categories.names } + onChange={ onCategoriesChange } + /> + ) } + { showTags && tags?.terms?.length > 0 && ( + ( { + id: tagId, + value: tags.mapById[ tagId ].name, + } ) ) } + suggestions={ tags.names } + onChange={ onTagsChange } + /> + ) } + + setQuery( { + author: value !== '' ? +value : undefined, } ) - ) } - suggestions={ categories.names } - onChange={ onCategoriesChange } + } /> - ) } - { showTags && tags?.terms?.length > 0 && ( - ( { - id: tagId, - value: tags.mapById[ tagId ].name, - } ) ) } - suggestions={ tags.names } - onChange={ onTagsChange } + - ) } - - setQuery( { - author: value !== '' ? +value : undefined, - } ) - } - /> - - + + ) } ); } diff --git a/packages/e2e-tests/fixtures/blocks/core__query.json b/packages/e2e-tests/fixtures/blocks/core__query.json index 235fcefff51e2..29d1251d872b9 100644 --- a/packages/e2e-tests/fixtures/blocks/core__query.json +++ b/packages/e2e-tests/fixtures/blocks/core__query.json @@ -5,6 +5,7 @@ "isValid": true, "attributes": { "query": { + "inherit": true, "perPage": null, "pages": 1, "offset": 0,