From 530c61a6aa76c30a09a5bd39132dfa08ceacf26c Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Mon, 10 Feb 2025 08:40:25 +0400 Subject: [PATCH] Query block: Add option to ignore sticky posts behavior (#69057) * Query block: Add option to ignore sticky posts behavior * Add backport changelog entry Unlinked contributors: keithdevon. Co-authored-by: Mamaduka Co-authored-by: carolinan Co-authored-by: markhowellsmead Co-authored-by: coreyworrell --- backport-changelog/6.8/8265.md | 3 +++ lib/compat/wordpress-6.8/blocks.php | 19 +++++++++++++++-- .../block-library/src/post-template/edit.js | 21 +++++++++++++------ .../edit/inspector-controls/sticky-control.js | 1 + 4 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 backport-changelog/6.8/8265.md diff --git a/backport-changelog/6.8/8265.md b/backport-changelog/6.8/8265.md new file mode 100644 index 00000000000000..46fa3f01810514 --- /dev/null +++ b/backport-changelog/6.8/8265.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/8265 + +* https://github.com/WordPress/gutenberg/pull/69057 diff --git a/lib/compat/wordpress-6.8/blocks.php b/lib/compat/wordpress-6.8/blocks.php index 1d27215762fe42..2b0fcd19e200e1 100644 --- a/lib/compat/wordpress-6.8/blocks.php +++ b/lib/compat/wordpress-6.8/blocks.php @@ -222,15 +222,30 @@ function gutenberg_update_ignored_hooked_blocks_postmeta( $post ) { * Update Query `parents` argument validation for hierarchical post types. * A zero is a valid parent ID for hierarchical post types. Used to display top-level items. * + * Add new handler for `sticky` query argument. + * * @param array $query The query vars. * @param WP_Block $block Block instance. * @return array The filtered query vars. */ -function gutenberg_parents_query_vars_from_query_block( $query, $block ) { +function gutenberg_update_query_vars_from_query_block_6_8( $query, $block ) { if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) { $query['post_parent__in'] = array_unique( array_map( 'intval', $block->context['query']['parents'] ) ); } + if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { + if ( 'ignore' === $block->context['query']['sticky'] ) { + $sticky = get_option( 'sticky_posts' ); + + /** + * The core will set `post__not_in` because it asserts that any sticky value other than `only` is `exclude`. + * Let's override that while supporting any `post__not_in` values outside sticky post logic. + */ + $query['post__not_in'] = array_diff( $query['post__not_in'], ! empty( $sticky ) ? $sticky : array() ); + $query['ignore_sticky_posts'] = 1; + } + } + return $query; } -add_filter( 'query_loop_block_query_vars', 'gutenberg_parents_query_vars_from_query_block', 10, 2 ); +add_filter( 'query_loop_block_query_vars', 'gutenberg_update_query_vars_from_query_block_6_8', 10, 2 ); diff --git a/packages/block-library/src/post-template/edit.js b/packages/block-library/src/post-template/edit.js index c58990233f3615..35d0a95501a1b2 100644 --- a/packages/block-library/src/post-template/edit.js +++ b/packages/block-library/src/post-template/edit.js @@ -177,18 +177,27 @@ export default function PostTemplateEdit( { query.format = format; } - // If sticky is not set, it will return all posts in the results. - // If sticky is set to `only`, it will limit the results to sticky posts only. - // If it is anything else, it will exclude sticky posts from results. For the record the value stored is `exclude`. - if ( sticky ) { + /* + * Handle cases where sticky is set to `exclude` or `only`. + * Which works as a `post__in/post__not_in` query for sticky posts. + */ + if ( sticky && sticky !== 'ignore' ) { query.sticky = sticky === 'only'; } + + if ( sticky === 'ignore' ) { + // Remove any leftover sticky query parameter. + delete query.sticky; + query.ignore_sticky = true; + } + // If `inherit` is truthy, adjust conditionally the query to create a better preview. + let currentPostType = postType; if ( inherit ) { // Change the post-type if needed. if ( templateSlug?.startsWith( 'archive-' ) ) { query.postType = templateSlug.replace( 'archive-', '' ); - postType = query.postType; + currentPostType = query.postType; } else if ( templateCategory ) { query.categories = templateCategory[ 0 ]?.id; } else if ( templateTag ) { @@ -205,7 +214,7 @@ export default function PostTemplateEdit( { } // When we preview Query Loop blocks we should prefer the current // block's postType, which is passed through block context. - const usedPostType = previewPostType || postType; + const usedPostType = previewPostType || currentPostType; return { posts: getEntityRecords( 'postType', usedPostType, { ...query, diff --git a/packages/block-library/src/query/edit/inspector-controls/sticky-control.js b/packages/block-library/src/query/edit/inspector-controls/sticky-control.js index ee7ee31ba977a9..f478337998f388 100644 --- a/packages/block-library/src/query/edit/inspector-controls/sticky-control.js +++ b/packages/block-library/src/query/edit/inspector-controls/sticky-control.js @@ -6,6 +6,7 @@ import { __ } from '@wordpress/i18n'; const stickyOptions = [ { label: __( 'Include' ), value: '' }, + { label: __( 'Ignore' ), value: 'ignore' }, { label: __( 'Exclude' ), value: 'exclude' }, { label: __( 'Only' ), value: 'only' }, ];