Skip to content

Post: Ensure get_post_custom_values() non-array returns grace fully.#11640

Open
liaisontw wants to merge 1 commit intoWordPress:trunkfrom
liaisontw:fix/65044-get-post-custom-values-handles-non-array
Open

Post: Ensure get_post_custom_values() non-array returns grace fully.#11640
liaisontw wants to merge 1 commit intoWordPress:trunkfrom
liaisontw:fix/65044-get-post-custom-values-handles-non-array

Conversation

@liaisontw
Copy link
Copy Markdown

Description
This PR improves the robustness of get_post_custom_values() by adding an explicit array check before accessing the data.

Currently, get_post_custom_values() assumes that get_post_custom() always returns an array. However, in certain scenarios (such as an invalid post_id or when no global post is set), get_post_custom() can return false. Attempting to access an array offset on a boolean value triggers a "Cannot use a scalar value as an array" warning in PHP 8.2+.

Changes included in this PR:

Array Validation: Added an is_array() check in get_post_custom_values() to safely handle non-array returns from get_post_custom().

Consistency: Aligns the behavior of get_post_custom_values() with get_post_custom_keys(), ensuring both return early when data is unavailable.

Unit Tests: Introduced a new test file tests/phpunit/tests/post/getPostCustom.php covering happy paths, invalid IDs, and empty key scenarios to prevent future regressions.

Trac ticket: https://core.trac.wordpress.org/ticket/65044

Use of AI Tools
AI assistance: Yes
Tool(s): Gemini
Model(s): Gemini 3 Flash
Used for: Structuring the PHPUnit test suite and drafting the technical justification for the PR. I have personally verified the behavior against PHP 8.2 and ensured the logic aligns with WordPress Core’s historical handling of post meta.

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.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 24, 2026

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 liaison, westonruter.

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

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

@liaisontw liaisontw force-pushed the fix/65044-get-post-custom-values-handles-non-array branch 2 times, most recently from e2c6b6d to 7e9f7a4 Compare April 24, 2026 05:15
Comment thread tests/phpunit/tests/post/getPostCustom.php
Comment thread tests/phpunit/tests/post/getPostCustom.php
Comment thread tests/phpunit/tests/post/getPostCustom.php Outdated
Comment thread tests/phpunit/tests/post/getPostCustom.php Outdated
Comment thread tests/phpunit/tests/post/getPostCustom.php Outdated
Comment thread tests/phpunit/tests/post/getPostCustom.php Outdated
@liaisontw liaisontw force-pushed the fix/65044-get-post-custom-values-handles-non-array branch from 7e9f7a4 to 94ce388 Compare April 27, 2026 03:29
@liaisontw liaisontw force-pushed the fix/65044-get-post-custom-values-handles-non-array branch from 94ce388 to 0f16735 Compare April 27, 2026 03:34
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.

2 participants