diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index e5b35e56cf..f97318e126 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -4285,7 +4285,7 @@ function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) function get_page_children($page_id, $pages) { $page_list = array(); foreach ( (array) $pages as $page ) { - if ( $page->post_parent == $page_id ) { + if ( $page->post_parent == $page_id || in_array( $page_id, $page->ancestors ) ) { $page_list[] = $page; if ( $children = get_page_children($page->ID, $pages) ) $page_list = array_merge($page_list, $children); @@ -4619,6 +4619,9 @@ function get_pages( $args = array() ) { // Update cache. update_post_cache( $pages ); + // Convert to WP_Post instances + $pages = array_map( 'get_post', $pages ); + if ( $child_of || $hierarchical ) { $pages = get_page_children($child_of, $pages); } @@ -4647,9 +4650,6 @@ function get_pages( $args = array() ) { wp_cache_set( $cache_key, $page_structure, 'posts' ); - // Convert to WP_Post instances. - $pages = array_map( 'get_post', $pages ); - /** * Filter the retrieved list of pages. * diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php index eb6e4840ec..3e351da7f7 100644 --- a/tests/phpunit/tests/post/getPages.php +++ b/tests/phpunit/tests/post/getPages.php @@ -268,4 +268,18 @@ class Tests_Post_getPages extends WP_UnitTestCase { $exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) ); $this->assertCount( 2, $exclude6 ); } + + /** + * @ticket 14477 + */ + function test_get_pages_interrupted_hierarchy() { + $page1 = $this->factory->post->create( array( 'post_type' => 'page' ) ); + $page2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page1 ) ); + add_post_meta( $page2, 'color', 'red' ); + $page3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page2 ) ); + add_post_meta( $page3, 'color', 'blue' ); + + $pages = get_pages( array( 'child_of' => $page1, 'meta_key' => 'color', 'meta_value' => 'blue' ) ); + $this->assertEqualSets( array( $page3 ), wp_list_pluck( $pages, 'ID' ) ); + } } \ No newline at end of file