Skip to content

Formatting: Fix wp_list_pluck() dropping index keys equal to zero - #13452

Open
haritpanchal wants to merge 2 commits into
WordPress:trunkfrom
haritpanchal:fix/57136-wp-list-pluck-zero-index-key
Open

Formatting: Fix wp_list_pluck() dropping index keys equal to zero#13452
haritpanchal wants to merge 2 commits into
WordPress:trunkfrom
haritpanchal:fix/57136-wp-list-pluck-zero-index-key

Conversation

@haritpanchal

Copy link
Copy Markdown

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)

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.

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
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props haritpanchal.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant