From 43302b45ee9064a8e0082259830c09d30b2c23e5 Mon Sep 17 00:00:00 2001 From: Shreya Shrivastava Date: Fri, 20 Nov 2026 16:45:32 +0530 Subject: [PATCH] Query: Make get_pages() child_of work with meta queries on interrupted hierarchies. See #14477. --- src/wp-includes/post.php | 36 +++++++++++++- tests/phpunit/tests/post/getPages.php | 67 +++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 3511fbfc81d7a..d1d3cdf2ed9bd 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -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 ); } diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php index a0a1d209957bd..df49eea0218cd 100644 --- a/tests/phpunit/tests/post/getPages.php +++ b/tests/phpunit/tests/post/getPages.php @@ -1204,4 +1204,71 @@ public function test_get_pages_order_by_post_modified_gmt() { '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( + array( + 'post_type' => 'page', + 'post_title' => 'Root', + ) + ); + $child = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_title' => 'Child', + 'post_parent' => $root, + ) + ); + $grandchild_a = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_title' => 'Grandchild A', + 'post_parent' => $child, + ) + ); + $grandchild_b = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_title' => 'Grandchild B', + 'post_parent' => $child, + ) + ); + $other_root = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_title' => 'Other root', + ) + ); + $outsider = self::factory()->post->create( + 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.' + ); + } }