Opened 4 years ago
Last modified 8 weeks ago
#57136 new defect (bug)
wp_list_pluck's key argument not working correctly when its zero
| Reported by: | dgwatkins | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | General | Version: | 4.7 |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description (last modified by )
This is better explained with an example:
wp> var_dump( wp_list_pluck( [ [ 'key1', 'val1' ], [ 'key2', 'val2' ] ], 1, 0 ));
array(2) {
[0]=>
string(4) "val1"
[1]=>
string(4) "val2"
}
See the missing key?
If we try with array_column it works correctly:
wp> var_dump( array_column( [ [ 'key1', 'val1' ], [ 'key2', 'val2' ] ], 1, 0 ));
array(2) {
["key1"]=>
string(4) "val1"
["key2"]=>
string(4) "val2"
}
Attachments (3)
Change History (14)
This ticket was mentioned in PR #3643 on WordPress/wordpress-develop by @riccardodicurti.
4 years ago
#1
- Keywords has-patch added
#3
@
4 years ago
Hi @afragen, may I ask what you mean with "related #56650". Should this ticket be closed? I don't see the same change in the other ticket.
#4
@
4 years ago
I said _related_ as the other ticket also deals with arguments to wp_list_pluck(). The other ticket goes about solving this issue a bit differently and also includes tests.
If you could test the PR for the ticket and see if it also fixes your issue that would be great. If it does, I would recommend closing this ticket as a duplicate.
#5
@
4 years ago
- Keywords needs-testing added
- Version → trunk
#6
@
4 years ago
- Version trunk → 4.7
#8
@
2 months ago
Hi @riccardodicurti — I reproduced this on current trunk. The existing PR still appears to have the right one-line direction, but it does not include regression tests and has had no code updates since 2022. Would you be comfortable with me preparing a current-trunk follow-up PR that preserves your contribution and adds coverage for numeric index key 0? I’m also happy to collaborate if you’re still working on it.
This ticket was mentioned in PR #13452 on WordPress/wordpress-develop by @haritpanchal.
2 months ago
#9
- Keywords has-unit-tests added
## Summary
WP_List_Util::pluck() used if ( ! $index_key ) to decide whether an index key was provided. Since 0 is falsy in PHP, passing 0 as $index_key was treated identically to not passing one at all, silently dropping the requested keys. array_column() — which this method's docblock says it mirrors — doesn't have this problem.
### Reproduction (from the ticket)
`php
var_dump( wp_list_pluck( [ [ 'key1', 'val1' ], [ 'key2', 'val2' ] ], 1, 0 ) );
Before: array(2) { [0]=> "val1" [1]=> "val2" } -- keys lost
After: array(2) { key1=> "val1" key2=> "val2" }
Changes
- wp-includes/class-wp-list-util.php: if ( ! $index_key ) → if ( null === $index_key ).
- Added two regression tests to tests/phpunit/tests/functions/wpListPluck.php covering array and object input, both with a 0 index key.
Testing
- Confirmed both new tests fail on unpatched trunk (reproducing the reported bug) and pass with the fix.
- Ran the full Tests_Functions_wpListPluck / WP_List_Util suite (87 tests) — all pass.
- Ran the taxonomy test group (903 tests, a heavy consumer of wp_list_pluck()) as a broader regression check — all pass.
- Verified no existing core call site passes 0 or as $index_key (grepped all 65 usages), so this fixes the bug with no behavior change to existing callers.
- phpcs clean on both changed files.
Trac ticket: https://core.trac.wordpress.org/ticket/57136
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Identifying the root cause via source review, implementing the fix, writing and verifying the regression tests (confirmed red→green), and running the broader test sweep. Reviewed and submitted by me.
#10
@
2 months ago
I've opened a PR for this: https://github.com/WordPress/wordpress-develop/pull/13452
The existing 2022 PR (#3643) had the right one-line fix direction (if ( ! $index_key ) → if ( null === $index_key )) but no regression tests and hasn't been updated for current trunk. This PR keeps that fix and adds two tests covering both the array and object cases from the original report, plus verification against the taxonomy test group and all existing wp_list_pluck() call sites in core (none rely on the old behavior with a 0 or index key).
#11
@
8 weeks ago
- Keywords needs-testing removed
Test Report
Description
This report validates whether the patch on ticket #57136 resolves the issue where passing 0 as the $index_key argument in wp_list_pluck() drops the specified index keys.
Patch tested: ticket:57136:57136.diff / PR #13452
Environment
- WordPress: 7.1
- PHP: 7.4.33 & 8.3
- OS: macOS
- Server: Local Environment
Steps to Reproduce
- Execute
wp_list_pluck()with array data using column index0as the$index_key:$list = array( array( 'key1', 'val1' ), array( 'key2', 'val2' ), ); print_r( wp_list_pluck( $list, 1, 0 ) );
- Also test with array of objects using property
0:$list = array( (object) array( 0 => 'key1', 1 => 'val1' ), (object) array( 0 => 'key2', 1 => 'val2' ), ); print_r( wp_list_pluck( $list, 1, 0 ) );
Expected Results
The returned array keys should be 'key1' and 'key2', matching the behavior of array_column( $list, 1, 0 ).
Actual Results
- ❌ Before Patch: Returns numeric array
[0 => 'val1', 1 => 'val2']. The keys at index0are lost becauseif ( ! $index_key )treats0as falsy. - ✅ After Patch (
null === $index_key): Returns['key1' => 'val1', 'key2' => 'val2']for both array and object inputs. - ✅ Calling
wp_list_pluck( $list, 1 )without an index key still works as expected, preserving default numeric indexing.


[[[
Trac ticket:
---
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.](https://core.trac.wordpress.org/ticket/57136)
](https://core.trac.wordpress.org/ticket/57136)](https://core.trac.wordpress.org/ticket/57136)