Make WordPress Core


Ignore:
Timestamp:
02/16/2026 01:11:50 PM (8 months ago)
Author:
ntsekouras
Message:

Editor: Update build_query_vars_from_query_block to handle new taxQuery structure.

The Query Loop block's taxQuery attribute now supports an include/exclude structure for term filtering. This updates the server-side query building to handle both the old format (e.g. {"category":[4]}) and the new format (e.g. {"include":{"category":[4]},"exclude":{"post_tag":[5]}}).

Props ntsekouras, westonruter, peterwilsoncc, mcsf.
Fixes #64416.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/wpBlock.php

    r61519 r61650  
    839839
    840840        /**
     841         * @ticket 64416
     842         */
     843        public function test_build_query_vars_from_query_block_tax_query_old_format() {
     844                $this->registry->register(
     845                        'core/example',
     846                        array( 'uses_context' => array( 'query' ) )
     847                );
     848
     849                $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
     850                $parsed_block  = $parsed_blocks[0];
     851                $context       = array(
     852                        'query' => array(
     853                                'taxQuery' => array(
     854                                        'category' => array( 1, 2, 3 ),
     855                                        'post_tag' => array( 10, 20 ),
     856                                ),
     857                        ),
     858                );
     859                $block         = new WP_Block( $parsed_block, $context, $this->registry );
     860                $query         = build_query_vars_from_query_block( $block, 1 );
     861
     862                $this->assertSame(
     863                        array(
     864                                'post_type'    => 'post',
     865                                'order'        => 'DESC',
     866                                'orderby'      => 'date',
     867                                'post__not_in' => array(),
     868                                'tax_query'    => array(
     869                                        array(
     870                                                'taxonomy'         => 'category',
     871                                                'terms'            => array( 1, 2, 3 ),
     872                                                'include_children' => false,
     873                                        ),
     874                                        array(
     875                                                'taxonomy'         => 'post_tag',
     876                                                'terms'            => array( 10, 20 ),
     877                                                'include_children' => false,
     878                                        ),
     879                                ),
     880                        ),
     881                        $query
     882                );
     883        }
     884
     885        /**
     886         * @ticket 64416
     887         */
     888        public function test_build_query_vars_from_query_block_tax_query_include_exclude() {
     889                $this->registry->register(
     890                        'core/example',
     891                        array( 'uses_context' => array( 'query' ) )
     892                );
     893
     894                $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
     895                $parsed_block  = $parsed_blocks[0];
     896                $context       = array(
     897                        'query' => array(
     898                                'taxQuery' => array(
     899                                        'include' => array(
     900                                                'category' => array( 1, 2, 3 ),
     901                                        ),
     902                                        'exclude' => array(
     903                                                'post_tag' => array( 15 ),
     904                                        ),
     905                                ),
     906                        ),
     907                );
     908                $block         = new WP_Block( $parsed_block, $context, $this->registry );
     909                $query         = build_query_vars_from_query_block( $block, 1 );
     910
     911                $this->assertSame(
     912                        array(
     913                                'post_type'    => 'post',
     914                                'order'        => 'DESC',
     915                                'orderby'      => 'date',
     916                                'post__not_in' => array(),
     917                                'tax_query'    => array(
     918                                        array(
     919                                                'taxonomy'         => 'category',
     920                                                'terms'            => array( 1, 2, 3 ),
     921                                                'operator'         => 'IN',
     922                                                'include_children' => false,
     923                                        ),
     924                                        array(
     925                                                'taxonomy'         => 'post_tag',
     926                                                'terms'            => array( 15 ),
     927                                                'operator'         => 'NOT IN',
     928                                                'include_children' => false,
     929                                        ),
     930                                ),
     931                        ),
     932                        $query
     933                );
     934        }
     935
     936        /**
    841937         * @ticket 62014
    842938         */
Note: See TracChangeset for help on using the changeset viewer.