Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ignore option to sticky options for Query Loop block #66222

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backport-changelog/6.8/7584.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/7584

* https://github.com/WordPress/gutenberg/pull/66222
28 changes: 28 additions & 0 deletions lib/compat/wordpress-6.7/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,31 @@
return $query;
}
add_filter( 'query_loop_block_query_vars', 'gutenberg_add_format_query_vars_to_query_loop_block', 10, 2 );

/**
* Adds `ignore` option for sticky posts to the Query block.
*
* @see 'query_loop_block_query_vars'
*
* @param array $query The query vars.
* @param WP_Block $block Block instance.
* @return array The filtered query vars.
*/
function gutenberg_add_ignore_sticky_posts_to_query_loop_block( $query, $block ) {
if ( ! empty( $block->context['query']['sticky'] ) && 'ignore' === $block->context['query']['sticky'] ) {
// Core function excludes all sticky posts if the `sticky` value is anything
// other than `only`. We must reset that here, but it could potentially also
// re-allow a sticky post that had been excluded in some other way. This

Check failure on line 154 in lib/compat/wordpress-6.7/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Whitespace found at end of line
// works okay for testing, but the real fix will need to be in the
// core function.
$sticky = get_option( 'sticky_posts' );

$query['post__not_in'] = array_diff( $query['post__not_in'], ! empty( $sticky ) ? $sticky : array() );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we exlude sticky posts? Ignore set below should include them and just list them in their natural order, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 125 doesn't exclude them, it's actually re-including them after core already excluded them. That's what the comment above line 123 is about.


// Ignore sticky posts.
$query['ignore_sticky_posts'] = 1;
}

return $query;
}
add_filter( 'query_loop_block_query_vars', 'gutenberg_add_ignore_sticky_posts_to_query_loop_block', 10, 2 );
28 changes: 28 additions & 0 deletions lib/compat/wordpress-6.8/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,31 @@
}

add_filter( 'register_block_type_args', 'gutenberg_stabilize_experimental_block_supports', PHP_INT_MAX, 1 );

/**
* Adds `ignore` option for sticky posts to the Query block.
*
* @see 'query_loop_block_query_vars'
*
* @param array $query The query vars.
* @param WP_Block $block Block instance.
* @return array The filtered query vars.
*/
function gutenberg_add_ignore_sticky_posts_to_query_loop_block( $query, $block ) {
if ( ! empty( $block->context['query']['sticky'] ) && 'ignore' === $block->context['query']['sticky'] ) {
// Core function excludes all sticky posts if the `sticky` value is anything
// other than `only`. We must reset that here, but it could potentially also
// re-allow a sticky post that had been excluded in some other way. This

Check failure on line 62 in lib/compat/wordpress-6.8/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Whitespace found at end of line
// works okay for testing, but the real fix will need to be in the
// core function.
$sticky = get_option( 'sticky_posts' );

$query['post__not_in'] = array_diff( $query['post__not_in'], ! empty( $sticky ) ? $sticky : array() );

// Ignore sticky posts.
$query['ignore_sticky_posts'] = 1;
}

return $query;
}
add_filter( 'query_loop_block_query_vars', 'gutenberg_add_ignore_sticky_posts_to_query_loop_block', 10, 2 );
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const stickyOptions = [
{ label: __( 'Include' ), value: '' },
{ label: __( 'Exclude' ), value: 'exclude' },
{ label: __( 'Only' ), value: 'only' },
{ label: __( 'Ignore' ), value: 'ignore' },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this affect the editor? Shouldn't there be some change to the querying done clientside in the editor too?

Copy link
Contributor

@carolinan carolinan Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question.

In the editor, the sticky posts are already bugged on trunk. For example sticky posts do not show as the first posts when the "include" option is set, which is the default. The result in the editor and front already does not match.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should at least try to see if we can match the preview in editor. As @carolinan says it's already buggy and if I remember correctly is about what REST API supports how it maps with WP_Query. That's probably the reason (the mapping) for not having this as a separate option for sticky control.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore label is not self explanatory in this context and is quite similar to the exclude, where it's ignore post stickiness.

Maybe a different control can be used with help texts? --cc @jameskoster

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps 'Ignore stickiness' is okay for now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore stickiness 😂

];

export default function StickyControl( { value, onChange } ) {
Expand Down
Loading