Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -6915,7 +6915,41 @@ static function ( $orderby_field ) {
$pages = new WP_Query();
$pages = $pages->query( $query_args );

if ( $child_of || $hierarchical ) {
if ( $child_of && ( '' !== (string) $meta_key || '' !== (string) $meta_value ) && empty( $parsed_args['include'] ) ) {
/*
* The meta query can match descendants whose own parents do not match,
* which would break the parent chain `get_page_children()` walks down.
* Temporarily add the missing ancestors so the tree is complete, then
* return only the pages that actually matched the query.
*/
$matched_ids = array_flip( wp_list_pluck( $pages, 'ID' ) );
$tree_pages = $pages;
$known_ids = $matched_ids;

foreach ( $pages as $page ) {
$ancestor_id = (int) $page->post_parent;

while ( $ancestor_id && $ancestor_id !== $child_of && ! isset( $known_ids[ $ancestor_id ] ) ) {
$ancestor = get_post( $ancestor_id );
if ( ! $ancestor ) {
break;
}

$known_ids[ $ancestor_id ] = true;
$tree_pages[] = $ancestor;
$ancestor_id = (int) $ancestor->post_parent;
}
}

$pages = array_values(
array_filter(
get_page_children( $child_of, $tree_pages ),
static function ( $page ) use ( $matched_ids ) {
return isset( $matched_ids[ $page->ID ] );
}
)
);
} elseif ( $child_of || $hierarchical ) {
$pages = get_page_children( $child_of, $pages );
}

Expand Down
67 changes: 67 additions & 0 deletions tests/phpunit/tests/post/getPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -1204,4 +1204,71 @@
'Check that ORDER is post modified when using modified_gmt.'
);
}

/**
* Tests that `child_of` finds matching descendants even when an intermediate page does not match the meta query.
*
* @ticket 14477
*/
public function test_get_pages_child_of_with_meta_and_interrupted_hierarchy() {
$root = self::factory()->post->create(

Check warning on line 1214 in tests/phpunit/tests/post/getPages.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space
array(
'post_type' => 'page',
'post_title' => 'Root',
)
);
$child = self::factory()->post->create(

Check warning on line 1220 in tests/phpunit/tests/post/getPages.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space
array(
'post_type' => 'page',

Check warning on line 1222 in tests/phpunit/tests/post/getPages.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 3 space(s) between "'post_type'" and double arrow, but found 2.
'post_title' => 'Child',

Check warning on line 1223 in tests/phpunit/tests/post/getPages.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 2 space(s) between "'post_title'" and double arrow, but found 1.
'post_parent' => $root,
)
);
$grandchild_a = self::factory()->post->create(
array(
'post_type' => 'page',

Check warning on line 1229 in tests/phpunit/tests/post/getPages.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 3 space(s) between "'post_type'" and double arrow, but found 2.
'post_title' => 'Grandchild A',

Check warning on line 1230 in tests/phpunit/tests/post/getPages.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 2 space(s) between "'post_title'" and double arrow, but found 1.
'post_parent' => $child,
)
);
$grandchild_b = self::factory()->post->create(
array(
'post_type' => 'page',

Check warning on line 1236 in tests/phpunit/tests/post/getPages.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 3 space(s) between "'post_type'" and double arrow, but found 2.
'post_title' => 'Grandchild B',

Check warning on line 1237 in tests/phpunit/tests/post/getPages.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 2 space(s) between "'post_title'" and double arrow, but found 1.
'post_parent' => $child,
)
);
$other_root = self::factory()->post->create(

Check warning on line 1241 in tests/phpunit/tests/post/getPages.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
array(
'post_type' => 'page',
'post_title' => 'Other root',
)
);
$outsider = self::factory()->post->create(

Check warning on line 1247 in tests/phpunit/tests/post/getPages.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
array(
'post_type' => 'page',
'post_title' => 'Outsider',
'post_parent' => $other_root,
)
);

// Only some descendants (and an unrelated page) have the meta; the intermediate `$child` does not.
foreach ( array( $grandchild_a, $grandchild_b, $outsider ) as $page_id ) {
add_post_meta( $page_id, 'foo', 'bar' );
}

$pages = get_pages(
array(
'child_of' => $root,
'meta_key' => 'foo',
'meta_value' => 'bar',
)
);

$this->assertSame(
array( $grandchild_a, $grandchild_b ),
wp_list_pluck( $pages, 'ID' ),
'Matching grandchildren should be returned, without non-matching intermediates or unrelated pages.'
);
}
}
Loading