Skip to content

Commit

Permalink
Editor: Fix parents argument validation for Query block.
Browse files Browse the repository at this point in the history
Allow passing zero (`0`) via the `parents` argument. It is a valid value for hierarchical post types, often used to display top-level items.

Props mamaduka, audrasjb, peterwilsoncc.
Fixes #62901.

git-svn-id: https://develop.svn.wordpress.org/trunk@59761 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
Mamaduka committed Feb 4, 2025
1 parent 39b293e commit 7d10dd7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2638,7 +2638,7 @@ static function ( $format ) {
$query['s'] = $block->context['query']['search'];
}
if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) {
$query['post_parent__in'] = array_filter( array_map( 'intval', $block->context['query']['parents'] ) );
$query['post_parent__in'] = array_unique( array_map( 'intval', $block->context['query']['parents'] ) );
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/phpunit/tests/blocks/wpBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,39 @@ public function test_build_query_vars_from_query_block_page_with_offset() {
);
}

/**
* @ticket 62901
*/
public function test_build_query_vars_from_query_block_with_top_level_parent() {
$this->registry->register(
'core/example',
array( 'uses_context' => array( 'query' ) )
);

$parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
$parsed_block = $parsed_blocks[0];
$context = array(
'query' => array(
'postType' => 'page',
'parents' => array( 0 ),
),
);
$block = new WP_Block( $parsed_block, $context, $this->registry );
$query = build_query_vars_from_query_block( $block, 1 );

$this->assertSame(
array(
'post_type' => 'page',
'order' => 'DESC',
'orderby' => 'date',
'post__not_in' => array(),
'tax_query' => array(),
'post_parent__in' => array( 0 ),
),
$query
);
}

/**
* @ticket 56467
*/
Expand Down

0 comments on commit 7d10dd7

Please sign in to comment.