From 0b28c0137d9f66ccedcfafbb17b697afd3887096 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 4 Sep 2013 20:55:05 +0000 Subject: [PATCH] Allow `get_pages()` to accept an `array` for the arg `parent`. Adds unit tests, some will fail until the next commit. Fixes #9470. git-svn-id: https://develop.svn.wordpress.org/trunk@25244 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 7 ++++++- tests/phpunit/tests/post.php | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 87aaeef190..be110d1511 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -3735,8 +3735,13 @@ function get_pages($args = '') { } - if ( $parent >= 0 ) + if ( is_array( $parent ) ) { + $post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) ); + if ( ! empty( $post_parent__in ) ) + $where .= " AND post_parent IN ($post_parent__in)"; + } elseif ( $parent >= 0 ) { $where .= $wpdb->prepare(' AND post_parent = %d ', $parent); + } if ( 1 == count( $post_status ) ) { $where_post_type = $wpdb->prepare( "post_type = %s AND post_status = %s", $post_type, array_shift( $post_status ) ); diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 948801a5d1..b604e7b2b0 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -823,4 +823,29 @@ class Tests_Post extends WP_UnitTestCase { sort( $exc_result ); $this->assertEquals( $inc, $exc_result ); } + + function test_get_pages_parent() { + $page_id1 = $this->factory->post->create( array( 'post_type' => 'page' ) ); + $page_id2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) ); + $page_id3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id2 ) ); + $page_id4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) ); + + $pages = get_pages( array( 'parent' => 0, 'hierarchical' => false ) ); + $this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) ); + + $pages = get_pages( array( 'parent' => $page_id1, 'hierarchical' => false ) ); + $this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) ); + + $pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ), 'hierarchical' => false ) ); + $this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) ); + + $pages = get_pages( array( 'parent' => 0 ) ); + $this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) ); + + $pages = get_pages( array( 'parent' => $page_id1 ) ); + $this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) ); + + $pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ) ) ); + $this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) ); + } }