diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index c09321edfa5bd..fd5c2d769b571 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2489,8 +2489,10 @@ function build_query_vars_from_query_block( $block, $page ) { */ $query['post__in'] = ! empty( $sticky ) ? $sticky : array( 0 ); $query['ignore_sticky_posts'] = 1; - } else { + } elseif ( 'exclude' === $block->context['query']['sticky'] ) { $query['post__not_in'] = array_merge( $query['post__not_in'], $sticky ); + } elseif ( 'ignore' === $block->context['query']['sticky'] ) { + $query['ignore_sticky_posts'] = 1; } } if ( ! empty( $block->context['query']['exclude'] ) ) { diff --git a/tests/phpunit/tests/blocks/wpBlock.php b/tests/phpunit/tests/blocks/wpBlock.php index bfde02b4dc44c..f28bbd6c83733 100644 --- a/tests/phpunit/tests/blocks/wpBlock.php +++ b/tests/phpunit/tests/blocks/wpBlock.php @@ -747,6 +747,149 @@ public function test_build_query_vars_from_query_block_with_top_level_parent() { ); } + /** + * Ensure requesting only sticky posts returns only sticky posts. + * + * @ticket 62908 + */ + public function test_build_query_vars_from_block_query_only_sticky_posts() { + $this->factory()->post->create_many( 5 ); + $sticky_post_id = $this->factory()->post->create( + array( + 'post_type' => 'post', + 'post_status' => 'publish', + 'post_title' => 'Sticky Post', + ) + ); + stick_post( $sticky_post_id ); + + $this->registry->register( + 'core/example', + array( 'uses_context' => array( 'query' ) ) + ); + + $parsed_blocks = parse_blocks( 'ab' ); + $parsed_block = $parsed_blocks[0]; + $context = array( + 'query' => array( + 'sticky' => 'only', + ), + ); + $block = new WP_Block( $parsed_block, $context, $this->registry ); + $query_args = build_query_vars_from_query_block( $block, 1 ); + + $this->assertSame( + array( + 'post_type' => 'post', + 'order' => 'DESC', + 'orderby' => 'date', + 'post__not_in' => array(), + 'tax_query' => array(), + 'post__in' => array( $sticky_post_id ), + 'ignore_sticky_posts' => 1, + ), + $query_args + ); + + $query = new WP_Query( $query_args ); + $this->assertSame( array( $sticky_post_id ), wp_list_pluck( $query->posts, 'ID' ) ); + } + + /** + * Ensure excluding sticky posts returns only non-sticky posts. + * + * @ticket 62908 + */ + public function test_build_query_vars_from_block_query_exclude_sticky_posts() { + $not_sticky_post_ids = $this->factory()->post->create_many( 5 ); + $sticky_post_id = $this->factory()->post->create( + array( + 'post_type' => 'post', + 'post_status' => 'publish', + 'post_title' => 'Sticky Post', + ) + ); + stick_post( $sticky_post_id ); + + $this->registry->register( + 'core/example', + array( 'uses_context' => array( 'query' ) ) + ); + + $parsed_blocks = parse_blocks( 'ab' ); + $parsed_block = $parsed_blocks[0]; + $context = array( + 'query' => array( + 'sticky' => 'exclude', + ), + ); + $block = new WP_Block( $parsed_block, $context, $this->registry ); + $query_args = build_query_vars_from_query_block( $block, 1 ); + + $this->assertSame( + array( + 'post_type' => 'post', + 'order' => 'DESC', + 'orderby' => 'date', + 'post__not_in' => array(), + 'tax_query' => array(), + 'post__not_in' => array( $sticky_post_id ), + ), + $query_args + ); + + $query = new WP_Query( $query_args ); + $this->assertNotContains( $sticky_post_id, wp_list_pluck( $query->posts, 'ID' ) ); + $this->assertSameSets( $not_sticky_post_ids, wp_list_pluck( $query->posts, 'ID' ) ); + } + + /** + * Ensure ignoring sticky posts includes both sticky and non-sticky posts. + * + * @ticket 62908 + */ + public function test_build_query_vars_from_block_query_ignore_sticky_posts() { + $not_sticky_post_ids = $this->factory()->post->create_many( 5 ); + $sticky_post_id = $this->factory()->post->create( + array( + 'post_type' => 'post', + 'post_status' => 'publish', + 'post_title' => 'Sticky Post', + ) + ); + stick_post( $sticky_post_id ); + + $this->registry->register( + 'core/example', + array( 'uses_context' => array( 'query' ) ) + ); + + $parsed_blocks = parse_blocks( 'ab' ); + $parsed_block = $parsed_blocks[0]; + $context = array( + 'query' => array( + 'sticky' => 'ignore', + ), + ); + $block = new WP_Block( $parsed_block, $context, $this->registry ); + $query_args = build_query_vars_from_query_block( $block, 1 ); + + $this->assertSame( + array( + 'post_type' => 'post', + 'order' => 'DESC', + 'orderby' => 'date', + 'post__not_in' => array(), + 'tax_query' => array(), + 'ignore_sticky_posts' => 1, + ), + $query_args + ); + + $query = new WP_Query( $query_args ); + $this->assertSameSets( array_merge( $not_sticky_post_ids, array( $sticky_post_id ) ), wp_list_pluck( $query->posts, 'ID' ) ); + } + /** * @ticket 56467 */