Opened 4 years ago
Last modified 5 days 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 needs-testing 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 (13)
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
Hi @afragen, I tested your PR and unfortunately it doesn't solve the problem reported in this ticket.
Ticket #56650
As you can see, the output is like the one reported in this ticket.
Ticket #57136
Using this patch the output is correct.
#6
@
4 years ago
- Version trunk → 4.7
#8
@
10 days 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.
6 days 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
@
5 days 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).
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)


[[[
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)