diff --git a/packages/block-library/src/query-loop/edit.js b/packages/block-library/src/query-loop/edit.js index c2be62707b2c56..8849d2fb32e6f1 100644 --- a/packages/block-library/src/query-loop/edit.js +++ b/packages/block-library/src/query-loop/edit.js @@ -67,7 +67,7 @@ export default function QueryLoopEdit( { query.exclude = exclude; } if ( sticky ) { - query.sticky = sticky; + query.sticky = sticky === 'show'; } return { posts: getEntityRecords( 'postType', postType, query ), diff --git a/packages/block-library/src/query-loop/index.php b/packages/block-library/src/query-loop/index.php index e8a0e0e4c9e203..a6bf2b5134a0ea 100644 --- a/packages/block-library/src/query-loop/index.php +++ b/packages/block-library/src/query-loop/index.php @@ -19,18 +19,27 @@ function render_block_core_query_loop( $attributes, $content, $block ) { $page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT ); $query = array( - 'post_type' => 'post', - 'offset' => 0, - 'order' => 'DESC', - 'orderby' => 'date', + 'post_type' => 'post', + 'offset' => 0, + 'order' => 'DESC', + 'orderby' => 'date', + 'post__not_in' => array(), ); if ( isset( $block->context['query'] ) ) { if ( isset( $block->context['query']['postType'] ) ) { $query['post_type'] = $block->context['query']['postType']; } + if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { + $sticky = get_option( 'sticky_posts' ); + if ( 'show' === $block->context['query']['sticky'] ) { + $query['post__in'] = $sticky; + } else { + $query['post__not_in'] = array_merge( $query['post__not_in'], $sticky ); + } + } if ( isset( $block->context['query']['exclude'] ) ) { - $query['post__not_in'] = $block->context['query']['exclude']; + $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']; @@ -56,9 +65,6 @@ function render_block_core_query_loop( $attributes, $content, $block ) { if ( isset( $block->context['query']['search'] ) ) { $query['s'] = $block->context['query']['search']; } - if ( isset( $block->context['query']['sticky'] ) && $block->context['query']['sticky'] ) { - $query['post__in'] = get_option( 'sticky_posts' ); - } } $posts = get_posts( $query ); diff --git a/packages/block-library/src/query/block.json b/packages/block-library/src/query/block.json index a89d52eba01168..f01c6b66081e35 100644 --- a/packages/block-library/src/query/block.json +++ b/packages/block-library/src/query/block.json @@ -20,7 +20,7 @@ "author": "", "search": "", "exclude": [], - "sticky": false + "sticky": "" } } }, 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 100fab36493937..8029ce9c46961c 100644 --- a/packages/block-library/src/query/edit/query-inspector-controls.js +++ b/packages/block-library/src/query/edit/query-inspector-controls.js @@ -13,7 +13,6 @@ import { TextControl, FormTokenField, SelectControl, - ToggleControl, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { InspectorControls } from '@wordpress/block-editor'; @@ -100,7 +99,7 @@ export default function QueryInspectorControls( { query, setQuery } ) { updateQuery.tagIds = []; } if ( newValue !== 'post' ) { - updateQuery.sticky = false; + updateQuery.sticky = ''; } setQuery( updateQuery ); }; @@ -125,6 +124,14 @@ export default function QueryInspectorControls( { query, setQuery } ) { onChangeDebounced(); return onChangeDebounced.cancel; }, [ querySearch, onChangeDebounced ] ); + const stickyOptions = useMemo( () => [ + { + label: __( 'All posts' ), + value: '', + }, + { label: __( 'Only sticky posts' ), value: 'show' }, + { label: __( 'Exclude sticky posts' ), value: 'exclude' }, + ] ); return ( @@ -176,11 +183,11 @@ export default function QueryInspectorControls( { query, setQuery } ) { onChange={ setQuerySearch } /> { showSticky && ( - setQuery( { sticky: value } ) } - help={ __( 'Limit results to items that are sticky.' ) } /> ) } diff --git a/packages/e2e-tests/fixtures/blocks/core__query.json b/packages/e2e-tests/fixtures/blocks/core__query.json index ad4fdc294296ab..3e90d6a3f9c0f9 100644 --- a/packages/e2e-tests/fixtures/blocks/core__query.json +++ b/packages/e2e-tests/fixtures/blocks/core__query.json @@ -16,7 +16,7 @@ "author": "", "search": "", "exclude": [], - "sticky": false + "sticky": "" } }, "innerBlocks": [],