From 5e9b89bf59943d4892c6f7825f1dd00f5176b3c0 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 3 Feb 2015 02:28:52 +0000 Subject: [PATCH] When using WP_Query's `'fields' => 'ids'` (or `'fields' => 'id=>parent'`), make sure the returned result is always an array of integers. fixes #31194. see #27252. git-svn-id: https://develop.svn.wordpress.org/trunk@31324 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 9 +++++++-- tests/phpunit/tests/query/results.php | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 3c1b0c2bdb..ddb655370a 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -3421,10 +3421,11 @@ class WP_Query { if ( 'ids' == $q['fields'] ) { $this->posts = $wpdb->get_col( $this->request ); + $this->posts = array_map( 'intval', $this->posts ); $this->post_count = count( $this->posts ); $this->set_found_posts( $q, $limits ); - return array_map( 'intval', $this->posts ); + return $this->posts; } if ( 'id=>parent' == $q['fields'] ) { @@ -3433,9 +3434,13 @@ class WP_Query { $this->set_found_posts( $q, $limits ); $r = array(); - foreach ( $this->posts as $post ) { + foreach ( $this->posts as $key => $post ) { + $this->posts[ $key ]->ID = (int) $post->ID; + $this->posts[ $key ]->post_parent = (int) $post->post_parent; + $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 ca74642439..45fdc1663a 100644 --- a/tests/phpunit/tests/query/results.php +++ b/tests/phpunit/tests/query/results.php @@ -387,6 +387,7 @@ class Tests_Query_Results extends WP_UnitTestCase { /** * @ticket 27252 + * @ticket 31194 */ function test_query_fields_integers() { @@ -401,6 +402,7 @@ class Tests_Query_Results extends WP_UnitTestCase { ) ); $this->assertSame( $parents, $posts1 ); + $this->assertSame( $parents, $this->q->posts ); $children = array( (int) self::$child_one => (int) self::$parent_one, @@ -415,6 +417,11 @@ class Tests_Query_Results extends WP_UnitTestCase { $this->assertSame( $children, $posts2 ); + foreach ( $this->q->posts as $post ) { + $this->assertInternalType( 'int', $post->ID ); + $this->assertInternalType( 'int', $post->post_parent ); + } + } /**