Skip to content

Feeds: Guard against empty array passed to max() in get_feed_build_date()#11387

Open
chubes4 wants to merge 3 commits intoWordPress:trunkfrom
chubes4:fix/59956-feed-build-date-empty-array
Open

Feeds: Guard against empty array passed to max() in get_feed_build_date()#11387
chubes4 wants to merge 3 commits intoWordPress:trunkfrom
chubes4:fix/59956-feed-build-date-empty-array

Conversation

@chubes4
Copy link
Copy Markdown

@chubes4 chubes4 commented Mar 29, 2026

Trac Ticket

See https://core.trac.wordpress.org/ticket/59956

Description

get_feed_build_date() passes the result of wp_list_pluck() directly to max() without checking for an empty array. When the query's post objects lack the post_modified_gmt property, wp_list_pluck() returns an empty array and max() throws a ValueError on PHP 8.0+.

This happens in production when bots request RSS feed URLs (e.g. /author/{name}/feed/, /taxonomy/{term}/feed/) for terms or authors with zero published posts. The have_posts() guard on line 722 passes (the query object exists and has a truthy post_count), but the underlying post objects are incomplete — so wp_list_pluck( $wp_query->posts, 'post_modified_gmt' ) returns [].

On PHP 7.x this was a silent warning. On PHP 8.0+ it is a fatal ValueError that returns HTTP 500 to the crawler.

The Fix

Wrap the max() call in if ( ! empty( $modified_times ) ). When the array is empty, $datetime remains false and falls through to the existing get_lastpostmodified( 'GMT' ) fallback on line 741. No behavioral change for feeds that have valid post data.

Testing

Automated: Added test_should_not_error_when_modified_times_is_empty to tests/phpunit/tests/date/getFeedBuildDate.php. The test simulates a query where have_posts() is true but posts lack post_modified_gmt, and verifies the function falls back to get_lastpostmodified() instead of throwing.

Manual (production): Verified on a WordPress 6.9.4 multisite (PHP 8.4) with 1,175 artist taxonomy terms that had zero published posts on the main site. Before the fix, these feed URLs returned HTTP 500 with ValueError: max(): Argument #1 ($value) must contain at least one element. After the fix, all return HTTP 200 with valid empty RSS feeds. Normal feeds with posts are unaffected.

  • Tested 12 edge cases: empty taxonomy feeds, populated category feeds, author feeds, comment feeds, main site feed, cross-subsite feeds
  • Monitored debug.log — fatal errors stopped immediately after applying the fix (were firing every 10–20 seconds prior)
  • Feeds with posts still return correct <item> elements and <lastBuildDate>

AI Disclosure

AI assistance: Yes
Tool(s): Claude Code (Claude Opus 4.6 directly on a VPS via OpenCode)
Used for: Identifying the root cause from production debug logs, drafting the guard condition, and scaffolding the unit test. Final implementation, testing, and PR description were reviewed and edited by a human contributor.

…te().

When `wp_list_pluck()` returns an empty array from posts that lack the
`post_modified_gmt` property, `max()` throws a `ValueError` on PHP 8+.

This adds an emptiness check before calling `max()`, allowing `$datetime`
to remain `false` and fall through to the existing `get_lastpostmodified()`
fallback.

Includes a unit test that simulates the condition.

Fixes #59956.
@github-actions
Copy link
Copy Markdown

Hi @chubes4! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

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

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.

chubes4 added 2 commits March 29, 2026 04:35
Aligns variable assignments in the test to satisfy the
Generic.Formatting.MultipleStatementAlignment sniff.
…_times.

The production failure occurs when the posts array contains scalar values
(e.g. post IDs) rather than post objects. wp_list_pluck() emits a
_doing_it_wrong notice and returns an empty array, which then triggers
the max() ValueError.

Update the test to use an integer in the posts array and call
setExpectedIncorrectUsage() to properly handle the expected notice.

See #59956.
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