Skip to content

Prevent fatal error in od_get_current_url_metrics_etag() when $wp_query->posts is null#2347

Merged
westonruter merged 3 commits intotrunkfrom
fix/null-wp-query-posts
Jan 15, 2026
Merged

Prevent fatal error in od_get_current_url_metrics_etag() when $wp_query->posts is null#2347
westonruter merged 3 commits intotrunkfrom
fix/null-wp-query-posts

Conversation

@westonruter
Copy link
Copy Markdown
Member

Summary

Fixes #2345

Relevant technical choices

It turns out that WP_Query::$posts is defined as:

	/**
	 * Array of post objects or post IDs.
	 *
	 * @since 1.5.0
	 * @var WP_Post[]|int[]|null
	 */
	public $posts;

Note: it can be null!

This ensures no error occurs in that case.

@westonruter westonruter added [Type] Bug An existing feature is broken [Plugin] Optimization Detective Issues for the Optimization Detective plugin labels Jan 14, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 14, 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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @sjlangley.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: sjlangley.

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: b1ink0 <b1ink0@git.wordpress.org>

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

@westonruter
Copy link
Copy Markdown
Member Author

westonruter commented Jan 14, 2026

Build for testing: optimization-detective.zip optimization-detective.zip

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.21%. Comparing base (c9720dc) to head (ff0c3a3).
⚠️ Report is 14 commits behind head on trunk.

Additional details and impacted files
@@           Coverage Diff           @@
##            trunk    #2347   +/-   ##
=======================================
  Coverage   69.21%   69.21%           
=======================================
  Files          90       90           
  Lines        7703     7703           
=======================================
  Hits         5332     5332           
  Misses       2371     2371           
Flag Coverage Δ
multisite 69.21% <100.00%> (ø)
single 35.41% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents a fatal error in od_get_current_url_metrics_etag() when WP_Query::$posts is null instead of an array. The fix addresses issue #2345 by adding a type check before passing the property to array_map().

Changes:

  • Updated the condition check from post_count > 0 to is_array( $wp_query->posts ) to handle null values
  • Added a test case to verify the function handles null $wp_query->posts without errors

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
plugins/optimization-detective/storage/data.php Changed condition to explicitly check if $wp_query->posts is an array before using it with array_map()
plugins/optimization-detective/tests/storage/test-data.php Added test case that simulates null $wp_query->posts by unsetting it

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@westonruter westonruter force-pushed the fix/null-wp-query-posts branch from 284b3e2 to ff0c3a3 Compare January 14, 2026 22:52
@westonruter westonruter enabled auto-merge January 14, 2026 22:53
@westonruter westonruter requested a review from b1ink0 January 14, 2026 22:54
@westonruter westonruter merged commit d502e07 into trunk Jan 15, 2026
29 of 30 checks passed
@westonruter westonruter deleted the fix/null-wp-query-posts branch January 15, 2026 18:59
@b1ink0
Copy link
Copy Markdown
Contributor

b1ink0 commented Jan 15, 2026

@westonruter was this PR supposed to be auto merged?

@westonruter
Copy link
Copy Markdown
Member Author

@b1ink0 yes, I enabled auto-merge.

@b1ink0
Copy link
Copy Markdown
Contributor

b1ink0 commented Jan 15, 2026

Oh okay, I just wanted to make sure it was internal.

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

Labels

[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Bug An existing feature is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHP Fatal Error in Optimization Detective on Publication Pages (array_map() argument type mismatch)

3 participants