Make WordPress Core

Changeset 25244


Ignore:
Timestamp:
09/04/2013 08:55:05 PM (13 years ago)
Author:
wonderboymusic
Message:

Allow get_pages() to accept an array for the arg parent. Adds unit tests, some will fail until the next commit.

Fixes #9470.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r25234 r25244  
    37363736        }
    37373737
    3738         if ( $parent >= 0 )
     3738        if ( is_array( $parent ) ) {
     3739                $post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) );
     3740                if ( ! empty( $post_parent__in ) )
     3741                        $where .= " AND post_parent IN ($post_parent__in)";
     3742        } elseif ( $parent >= 0 ) {
    37393743                $where .= $wpdb->prepare(' AND post_parent = %d ', $parent);
     3744        }
    37403745
    37413746        if ( 1 == count( $post_status ) ) {
  • trunk/tests/phpunit/tests/post.php

    r25168 r25244  
    824824                $this->assertEquals( $inc, $exc_result );
    825825        }
     826
     827        function test_get_pages_parent() {
     828                $page_id1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
     829                $page_id2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
     830                $page_id3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id2 ) );
     831                $page_id4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
     832
     833                $pages = get_pages( array( 'parent' => 0, 'hierarchical' => false ) );
     834                $this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
     835
     836                $pages = get_pages( array( 'parent' => $page_id1, 'hierarchical' => false ) );
     837                $this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
     838
     839                $pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ), 'hierarchical' => false ) );
     840                $this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
     841
     842                $pages = get_pages( array( 'parent' => 0 ) );
     843                $this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
     844
     845                $pages = get_pages( array( 'parent' => $page_id1 ) );
     846                $this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
     847
     848                $pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ) ) );
     849                $this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
     850        }
    826851}
Note: See TracChangeset for help on using the changeset viewer.