Make WordPress Core

Changeset 59866


Ignore:
Timestamp:
02/25/2025 05:10:31 AM (12 hours ago)
Author:
Mamaduka
Message:

Editor: Add option to ignore sticky posts in Query block.

Introduce a new ignore value for the sticky query argument. When this value is used, the query will not prepend sticky posts at the top but display them in the natural order.

Props mamaduka, peterwilsoncc, audrasjb, mikinc860, poena, dhruvishah2203, joemcgill.
Fixes #62908.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r59839 r59866  
    25652565                $query['post__in']            = ! empty( $sticky ) ? $sticky : array( 0 );
    25662566                $query['ignore_sticky_posts'] = 1;
    2567             } else {
     2567            } elseif ( 'exclude' === $block->context['query']['sticky'] ) {
    25682568                $query['post__not_in'] = array_merge( $query['post__not_in'], $sticky );
     2569            } elseif ( 'ignore' === $block->context['query']['sticky'] ) {
     2570                $query['ignore_sticky_posts'] = 1;
    25692571            }
    25702572        }
  • trunk/tests/phpunit/tests/blocks/wpBlock.php

    r59761 r59866  
    749749
    750750    /**
     751     * Ensure requesting only sticky posts returns only sticky posts.
     752     *
     753     * @ticket 62908
     754     */
     755    public function test_build_query_vars_from_block_query_only_sticky_posts() {
     756        $this->factory()->post->create_many( 5 );
     757        $sticky_post_id = $this->factory()->post->create(
     758            array(
     759                'post_type'   => 'post',
     760                'post_status' => 'publish',
     761                'post_title'  => 'Sticky Post',
     762            )
     763        );
     764        stick_post( $sticky_post_id );
     765
     766        $this->registry->register(
     767            'core/example',
     768            array( 'uses_context' => array( 'query' ) )
     769        );
     770
     771        $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
     772        $parsed_block  = $parsed_blocks[0];
     773        $context       = array(
     774            'query' => array(
     775                'sticky' => 'only',
     776            ),
     777        );
     778        $block         = new WP_Block( $parsed_block, $context, $this->registry );
     779        $query_args    = build_query_vars_from_query_block( $block, 1 );
     780
     781        $this->assertSame(
     782            array(
     783                'post_type'           => 'post',
     784                'order'               => 'DESC',
     785                'orderby'             => 'date',
     786                'post__not_in'        => array(),
     787                'tax_query'           => array(),
     788                'post__in'            => array( $sticky_post_id ),
     789                'ignore_sticky_posts' => 1,
     790            ),
     791            $query_args
     792        );
     793
     794        $query = new WP_Query( $query_args );
     795        $this->assertSame( array( $sticky_post_id ), wp_list_pluck( $query->posts, 'ID' ) );
     796    }
     797
     798    /**
     799     * Ensure excluding sticky posts returns only non-sticky posts.
     800     *
     801     * @ticket 62908
     802     */
     803    public function test_build_query_vars_from_block_query_exclude_sticky_posts() {
     804        $not_sticky_post_ids = $this->factory()->post->create_many( 5 );
     805        $sticky_post_id      = $this->factory()->post->create(
     806            array(
     807                'post_type'   => 'post',
     808                'post_status' => 'publish',
     809                'post_title'  => 'Sticky Post',
     810            )
     811        );
     812        stick_post( $sticky_post_id );
     813
     814        $this->registry->register(
     815            'core/example',
     816            array( 'uses_context' => array( 'query' ) )
     817        );
     818
     819        $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
     820        $parsed_block  = $parsed_blocks[0];
     821        $context       = array(
     822            'query' => array(
     823                'sticky' => 'exclude',
     824            ),
     825        );
     826        $block         = new WP_Block( $parsed_block, $context, $this->registry );
     827        $query_args    = build_query_vars_from_query_block( $block, 1 );
     828
     829        $this->assertSame(
     830            array(
     831                'post_type'    => 'post',
     832                'order'        => 'DESC',
     833                'orderby'      => 'date',
     834                'post__not_in' => array(),
     835                'tax_query'    => array(),
     836                'post__not_in' => array( $sticky_post_id ),
     837            ),
     838            $query_args
     839        );
     840
     841        $query = new WP_Query( $query_args );
     842        $this->assertNotContains( $sticky_post_id, wp_list_pluck( $query->posts, 'ID' ) );
     843        $this->assertSameSets( $not_sticky_post_ids, wp_list_pluck( $query->posts, 'ID' ) );
     844    }
     845
     846    /**
     847     * Ensure ignoring sticky posts includes both sticky and non-sticky posts.
     848     *
     849     * @ticket 62908
     850     */
     851    public function test_build_query_vars_from_block_query_ignore_sticky_posts() {
     852        $not_sticky_post_ids = $this->factory()->post->create_many( 5 );
     853        $sticky_post_id      = $this->factory()->post->create(
     854            array(
     855                'post_type'   => 'post',
     856                'post_status' => 'publish',
     857                'post_title'  => 'Sticky Post',
     858            )
     859        );
     860        stick_post( $sticky_post_id );
     861
     862        $this->registry->register(
     863            'core/example',
     864            array( 'uses_context' => array( 'query' ) )
     865        );
     866
     867        $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
     868        $parsed_block  = $parsed_blocks[0];
     869        $context       = array(
     870            'query' => array(
     871                'sticky' => 'ignore',
     872            ),
     873        );
     874        $block         = new WP_Block( $parsed_block, $context, $this->registry );
     875        $query_args    = build_query_vars_from_query_block( $block, 1 );
     876
     877        $this->assertSame(
     878            array(
     879                'post_type'           => 'post',
     880                'order'               => 'DESC',
     881                'orderby'             => 'date',
     882                'post__not_in'        => array(),
     883                'tax_query'           => array(),
     884                'ignore_sticky_posts' => 1,
     885            ),
     886            $query_args
     887        );
     888
     889        $query = new WP_Query( $query_args );
     890        $this->assertSameSets( array_merge( $not_sticky_post_ids, array( $sticky_post_id ) ), wp_list_pluck( $query->posts, 'ID' ) );
     891    }
     892
     893    /**
    751894     * @ticket 56467
    752895     */
Note: See TracChangeset for help on using the changeset viewer.