From 0cd55781ae24655ec0ef02df519cb2abb47e6b8f Mon Sep 17 00:00:00 2001 From: haritpanchal Date: Wed, 9 Sep 2026 12:13:38 +0530 Subject: [PATCH] Formatting: Fix wp_list_pluck() dropping index keys equal to zero. WP_List_Util::pluck() checked `if ( ! $index_key )` to detect whether an index key was requested, treating 0 the same as null since 0 is falsy in PHP. This silently discarded a valid numeric index key of 0, unlike array_column(), which this method is documented to mirror. Includes regression tests for the array and object cases. Trac ticket: https://core.trac.wordpress.org/ticket/57136 --- src/wp-includes/class-wp-list-util.php | 2 +- tests/phpunit/tests/functions/wpListPluck.php | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-list-util.php b/src/wp-includes/class-wp-list-util.php index 656035529186e..feb4a24b43387 100644 --- a/src/wp-includes/class-wp-list-util.php +++ b/src/wp-includes/class-wp-list-util.php @@ -158,7 +158,7 @@ public function filter( $args = array(), $operator = 'AND' ) { public function pluck( $field, $index_key = null ) { $newlist = array(); - if ( ! $index_key ) { + if ( null === $index_key ) { /* * This is simple. Could at some point wrap array_column() * if we knew we had an array of arrays. diff --git a/tests/phpunit/tests/functions/wpListPluck.php b/tests/phpunit/tests/functions/wpListPluck.php index 8313116294519..fa9ad191b3c66 100644 --- a/tests/phpunit/tests/functions/wpListPluck.php +++ b/tests/phpunit/tests/functions/wpListPluck.php @@ -105,6 +105,54 @@ public function test_wp_list_pluck_object_index_key() { ); } + /** + * @ticket 57136 + */ + public function test_wp_list_pluck_index_key_of_zero() { + $list = wp_list_pluck( + array( + array( 'key1', 'val1' ), + array( 'key2', 'val2' ), + ), + 1, + 0 + ); + $this->assertSame( + array( + 'key1' => 'val1', + 'key2' => 'val2', + ), + $list + ); + } + + /** + * @ticket 57136 + */ + public function test_wp_list_pluck_object_index_key_of_zero() { + $list = wp_list_pluck( + array( + (object) array( + 0 => 'key1', + 1 => 'val1', + ), + (object) array( + 0 => 'key2', + 1 => 'val2', + ), + ), + 1, + 0 + ); + $this->assertSame( + array( + 'key1' => 'val1', + 'key2' => 'val2', + ), + $list + ); + } + /** * @ticket 28666 */