From 710f11f2dfbce6d4cf5ea4fab92e80222e925367 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 24 Mar 2014 18:54:38 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@27686 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 10 +++---- tests/phpunit/tests/query/results.php | 40 ++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 2adf7d2168..05d4979d4f 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -2268,7 +2268,7 @@ class WP_Query { // This overrides posts_per_page. if ( ! empty( $q['posts_per_rss'] ) ) { $q['posts_per_page'] = $q['posts_per_rss']; - } else { + } else { $q['posts_per_page'] = get_option( 'posts_per_rss' ); } $q['nopaging'] = false; @@ -3205,7 +3205,7 @@ class WP_Query { $this->post_count = count( $this->posts ); $this->set_found_posts( $q, $limits ); - return $this->posts; + return array_map( 'intval', $this->posts ); } if ( 'id=>parent' == $q['fields'] ) { @@ -3214,9 +3214,9 @@ class WP_Query { $this->set_found_posts( $q, $limits ); $r = array(); - foreach ( $this->posts as $post ) - $r[ $post->ID ] = $post->post_parent; - + foreach ( $this->posts as $post ) { + $r[ (int) $post->ID ] = (int) $post->post_parent; + } return $r; } diff --git a/tests/phpunit/tests/query/results.php b/tests/phpunit/tests/query/results.php index 4593c14311..20c45ba446 100644 --- a/tests/phpunit/tests/query/results.php +++ b/tests/phpunit/tests/query/results.php @@ -46,10 +46,10 @@ class Tests_Query_Results extends WP_UnitTestCase { $this->parent_one = $this->factory->post->create( array( 'post_title' => 'parent-one', 'post_date' => '2007-01-01 00:00:00' ) ); $this->parent_two = $this->factory->post->create( array( 'post_title' => 'parent-two', 'post_date' => '2007-01-01 00:00:00' ) ); $this->parent_three = $this->factory->post->create( array( 'post_title' => 'parent-three', 'post_date' => '2007-01-01 00:00:00' ) ); - $this->factory->post->create( array( 'post_title' => 'child-one', 'post_parent' => $this->parent_one, 'post_date' => '2007-01-01 00:00:01' ) ); - $this->factory->post->create( array( 'post_title' => 'child-two', 'post_parent' => $this->parent_one, 'post_date' => '2007-01-01 00:00:02' ) ); - $this->factory->post->create( array( 'post_title' => 'child-three', 'post_parent' => $this->parent_two, 'post_date' => '2007-01-01 00:00:03' ) ); - $this->factory->post->create( array( 'post_title' => 'child-four', 'post_parent' => $this->parent_two, 'post_date' => '2007-01-01 00:00:04' ) ); + $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' ) ); + $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' ) ); + $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' ) ); + $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' ) ); unset( $this->q ); $this->q = new WP_Query(); @@ -370,6 +370,38 @@ class Tests_Query_Results extends WP_UnitTestCase { ), wp_list_pluck( $posts, 'post_title' ) ); } + /** + * @ticket 27252 + */ + function test_query_fields_integers() { + + $parents = array( + (int) $this->parent_one, + (int) $this->parent_two + ); + $posts1 = $this->q->query( array( + 'post__in' => $parents, + 'fields' => 'ids', + 'orderby' => 'post__in', + ) ); + + $this->assertSame( $parents, $posts1 ); + + $children = array( + (int) $this->child_one => (int) $this->parent_one, + (int) $this->child_two => (int) $this->parent_one + ); + + $posts2 = $this->q->query( array( + 'post__in' => array_keys( $children ), + 'fields' => 'id=>parent', + 'orderby' => 'post__in', + ) ); + + $this->assertSame( $children, $posts2 ); + + } + function test_exlude_from_search_empty() { global $wp_post_types; foreach ( array_keys( $wp_post_types ) as $slug )