WordPress.org

Make WordPress Core

Changeset 27686


Ignore:
Timestamp:
03/24/2014 06:54:38 PM (8 years ago)
Author:
wonderboymusic
Message:

When using WP_Query's "fields" => "ids" (or "fields" => "id=>parent"), the returned values should be an array of integers, not array of integers represented by strings.

Adds unit tests. All other unit tests pass.

Props danielbachhuber.
Fixes #27252.

Location:
trunk
Files:
2 edited

Legend:

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

    r27633 r27686  
    22692269            if ( ! empty( $q['posts_per_rss'] ) ) {
    22702270                $q['posts_per_page'] = $q['posts_per_rss'];
    2271             } else { 
     2271            } else {
    22722272                $q['posts_per_page'] = get_option( 'posts_per_rss' );
    22732273            }
     
    32063206            $this->set_found_posts( $q, $limits );
    32073207
    3208             return $this->posts;
     3208            return array_map( 'intval', $this->posts );
    32093209        }
    32103210
     
    32153215
    32163216            $r = array();
    3217             foreach ( $this->posts as $post )
    3218                 $r[ $post->ID ] = $post->post_parent;
    3219 
     3217            foreach ( $this->posts as $post ) {
     3218                $r[ (int) $post->ID ] = (int) $post->post_parent;
     3219            }
    32203220            return $r;
    32213221        }
  • trunk/tests/phpunit/tests/query/results.php

    r27395 r27686  
    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 );
     
    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        $posts1 = $this->q->query( array(
     383            'post__in'  => $parents,
     384            'fields'    => 'ids',
     385            'orderby'   => 'post__in',
     386        ) );
     387
     388        $this->assertSame( $parents, $posts1 );
     389
     390        $children = array(
     391            (int) $this->child_one => (int) $this->parent_one,
     392            (int) $this->child_two => (int) $this->parent_one
     393        );
     394
     395        $posts2 = $this->q->query( array(
     396            'post__in'  => array_keys( $children ),
     397            'fields'    => 'id=>parent',
     398            'orderby'   => 'post__in',
     399        ) );
     400
     401        $this->assertSame( $children, $posts2 );
     402
     403    }
     404
    373405    function test_exlude_from_search_empty() {
    374406        global $wp_post_types;
Note: See TracChangeset for help on using the changeset viewer.