Skip to content

Commit

Permalink
Query block: Add option to ignore sticky posts behavior (#69057)
Browse files Browse the repository at this point in the history
* Query block: Add option to ignore sticky posts behavior
* Add backport changelog entry

Unlinked contributors: keithdevon.

Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: carolinan <[email protected]>
Co-authored-by: markhowellsmead <[email protected]>
Co-authored-by: coreyworrell <[email protected]>
  • Loading branch information
5 people authored Feb 10, 2025
1 parent aa89fdc commit 530c61a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
3 changes: 3 additions & 0 deletions backport-changelog/6.8/8265.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/8265

* https://github.com/WordPress/gutenberg/pull/69057
19 changes: 17 additions & 2 deletions lib/compat/wordpress-6.8/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
21 changes: 15 additions & 6 deletions packages/block-library/src/post-template/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
];
Expand Down

0 comments on commit 530c61a

Please sign in to comment.