Skip to content

Commit 7d10dd7

Browse files
committed
Editor: Fix parents argument validation for Query block.
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
1 parent 39b293e commit 7d10dd7

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/wp-includes/blocks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2638,7 +2638,7 @@ static function ( $format ) {
26382638
$query['s'] = $block->context['query']['search'];
26392639
}
26402640
if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) {
2641-
$query['post_parent__in'] = array_filter( array_map( 'intval', $block->context['query']['parents'] ) );
2641+
$query['post_parent__in'] = array_unique( array_map( 'intval', $block->context['query']['parents'] ) );
26422642
}
26432643
}
26442644

tests/phpunit/tests/blocks/wpBlock.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,39 @@ public function test_build_query_vars_from_query_block_page_with_offset() {
714714
);
715715
}
716716

717+
/**
718+
* @ticket 62901
719+
*/
720+
public function test_build_query_vars_from_query_block_with_top_level_parent() {
721+
$this->registry->register(
722+
'core/example',
723+
array( 'uses_context' => array( 'query' ) )
724+
);
725+
726+
$parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
727+
$parsed_block = $parsed_blocks[0];
728+
$context = array(
729+
'query' => array(
730+
'postType' => 'page',
731+
'parents' => array( 0 ),
732+
),
733+
);
734+
$block = new WP_Block( $parsed_block, $context, $this->registry );
735+
$query = build_query_vars_from_query_block( $block, 1 );
736+
737+
$this->assertSame(
738+
array(
739+
'post_type' => 'page',
740+
'order' => 'DESC',
741+
'orderby' => 'date',
742+
'post__not_in' => array(),
743+
'tax_query' => array(),
744+
'post_parent__in' => array( 0 ),
745+
),
746+
$query
747+
);
748+
}
749+
717750
/**
718751
* @ticket 56467
719752
*/

0 commit comments

Comments
 (0)