diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
index 1dbf0c9..a629623 100644
a
|
b
|
function get_page_children( $page_id, $pages ) { |
4457 | 4457 | } |
4458 | 4458 | |
4459 | 4459 | /** |
| 4460 | * Retrieve all descendants of a given page ID, in a flat list. |
| 4461 | * Respects heirarchy, then alphabetical. |
| 4462 | * Needs a better name. |
| 4463 | * |
| 4464 | * @since 4.9 |
| 4465 | * |
| 4466 | * @param int $page_id Page ID. |
| 4467 | * @return array List of page children. |
| 4468 | */ |
| 4469 | |
| 4470 | function get_page_descendants( $page_id ) { |
| 4471 | $descendants = array(); |
| 4472 | |
| 4473 | $to_look = get_posts( array ( |
| 4474 | 'post_parent' => ($page_id !== false ? $page_id : -1), |
| 4475 | 'post_type' => 'page', |
| 4476 | 'posts_per_page' => -1 |
| 4477 | ) ); |
| 4478 | |
| 4479 | $descendants = $to_look; |
| 4480 | |
| 4481 | foreach($to_look as $index => $descendant) { |
| 4482 | $next_descendants = get_page_descendants( $descendant->ID ); |
| 4483 | array_splice($descendants, $index, 0, $next_descendants); |
| 4484 | } |
| 4485 | |
| 4486 | $descendants = array_unique($descendants, SORT_REGULAR); |
| 4487 | |
| 4488 | return $descendants; |
| 4489 | } |
| 4490 | /** |
4460 | 4491 | * Order the pages with children under parents in a flat list. |
4461 | 4492 | * |
4462 | 4493 | * It uses auxiliary structure to hold parent-children relationships and |
… |
… |
function get_pages( $args = array() ) { |
4804 | 4835 | update_post_cache( $pages ); |
4805 | 4836 | |
4806 | 4837 | if ( $child_of || $hierarchical ) { |
4807 | | $pages = get_page_children($child_of, $pages); |
| 4838 | $descendants = get_page_descendants($child_of); |
| 4839 | foreach($pages as $index => $page) { |
| 4840 | if( !in_array($page->ID, wp_list_pluck($descendants, 'ID'))) { |
| 4841 | unset($pages[$index]); |
| 4842 | } |
| 4843 | } |
4808 | 4844 | } |
4809 | 4845 | |
4810 | 4846 | if ( ! empty( $r['exclude_tree'] ) ) { |
diff --git a/tests/phpunit/tests/post/getPages.php b/tests/phpunit/tests/post/getPages.php
index b4bdd17..ed0588b 100644
a
|
b
|
class Tests_Post_getPages extends WP_UnitTestCase { |
509 | 509 | $exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) ); |
510 | 510 | $this->assertCount( 2, $exclude6 ); |
511 | 511 | } |
| 512 | |
| 513 | /** |
| 514 | * @ticket 14477 |
| 515 | */ |
| 516 | // function test_get_pages_interrupted_hierarchy() { |
| 517 | // $page1 = $this->factory->post->create( array( 'post_type' => 'page' ) ); |
| 518 | // $page2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page1 ) ); |
| 519 | // add_post_meta( $page2, 'color', 'red' ); |
| 520 | // $page3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page2 ) ); |
| 521 | // add_post_meta( $page3, 'color', 'blue' ); |
| 522 | // |
| 523 | // $pages = get_pages( array( 'child_of' => $page1, 'meta_key' => 'color', 'meta_value' => 'blue' ) ); |
| 524 | // |
| 525 | // $this->assertEqualSets( array( $page3 ), wp_list_pluck( $pages, 'ID' ) ); |
| 526 | // } |
512 | 527 | } |