WordPress.org

Make WordPress Core

Ticket #27252: 27252.1.diff

File 27252.1.diff, 3.4 KB (added by danielbachhuber, 8 years ago)

Cast returned post IDs as integers

  • src/wp-includes/query.php

     
    31903190                        $this->post_count = count( $this->posts );
    31913191                        $this->set_found_posts( $q, $limits );
    31923192
    3193                         return $this->posts;
     3193                        return array_map( 'intval', $this->posts );
    31943194                }
    31953195
    31963196                if ( 'id=>parent' == $q['fields'] ) {
     
    32003200
    32013201                        $r = array();
    32023202                        foreach ( $this->posts as $post )
    3203                                 $r[ $post->ID ] = $post->post_parent;
     3203                                $r[ (int) $post->ID ] = (int) $post->post_parent;
    32043204
    32053205                        return $r;
    32063206                }
  • tests/phpunit/tests/query/results.php

     
    4646                $this->parent_one = $this->factory->post->create( array( 'post_title' => 'parent-one', 'post_date' => '2007-01-01 00:00:00' ) );
    4747                $this->parent_two = $this->factory->post->create( array( 'post_title' => 'parent-two', 'post_date' => '2007-01-01 00:00:00' ) );
    4848                $this->parent_three = $this->factory->post->create( array( 'post_title' => 'parent-three', 'post_date' => '2007-01-01 00:00:00' ) );
    49                 $this->factory->post->create( array( 'post_title' => 'child-one', 'post_parent' => $this->parent_one, 'post_date' => '2007-01-01 00:00:01' ) );
    50                 $this->factory->post->create( array( 'post_title' => 'child-two', 'post_parent' => $this->parent_one, 'post_date' => '2007-01-01 00:00:02' ) );
    51                 $this->factory->post->create( array( 'post_title' => 'child-three', 'post_parent' => $this->parent_two, 'post_date' => '2007-01-01 00:00:03' ) );
    52                 $this->factory->post->create( array( 'post_title' => 'child-four', 'post_parent' => $this->parent_two, 'post_date' => '2007-01-01 00:00:04' ) );
     49                $this->child_one = $this->factory->post->create( array( 'post_title' => 'child-one', 'post_parent' => $this->parent_one, 'post_date' => '2007-01-01 00:00:01' ) );
     50                $this->child_two = $this->factory->post->create( array( 'post_title' => 'child-two', 'post_parent' => $this->parent_one, 'post_date' => '2007-01-01 00:00:02' ) );
     51                $this->child_three = $this->factory->post->create( array( 'post_title' => 'child-three', 'post_parent' => $this->parent_two, 'post_date' => '2007-01-01 00:00:03' ) );
     52                $this->child_four = $this->factory->post->create( array( 'post_title' => 'child-four', 'post_parent' => $this->parent_two, 'post_date' => '2007-01-01 00:00:04' ) );
    5353
    5454                unset( $this->q );
    5555                $this->q = new WP_Query();
     
    370370                ), wp_list_pluck( $posts, 'post_title' ) );
    371371        }
    372372
     373        /**
     374         * @ticket 27252
     375         */
     376        function test_query_fields_integers() {
     377
     378                $parents = array(
     379                        (int) $this->parent_one,
     380                        (int) $this->parent_two
     381                );
     382                $posts = $this->q->query( array(
     383                        'post__in'  => $parents,
     384                        'fields'    => 'ids',
     385                        'orderby'   => 'post__in',
     386                        ) );
     387
     388                $this->assertSame( $parents, $posts );
     389
     390                $children = array(
     391                        (int) $this->child_one => (int) $this->parent_one,
     392                        (int) $this->child_two => (int) $this->parent_one
     393                );
     394                $posts = $this->q->query( array(
     395                        'post__in'  => array_keys( $children ),
     396                        'fields'    => 'id=>parent',
     397                        'orderby'   => 'post__in',
     398                        ) );
     399
     400                $this->assertSame( $children, $posts );
     401
     402        }
     403
    373404        function test_exlude_from_search_empty() {
    374405                global $wp_post_types;
    375406                foreach ( array_keys( $wp_post_types ) as $slug )