From df5c561fa5060698abf4ef236630e4d6bedd7616 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 13 Jan 2026 23:28:26 -0800 Subject: [PATCH 1/2] Prevent fatal error when $wp_query->posts is null --- plugins/optimization-detective/storage/data.php | 6 +++++- .../optimization-detective/tests/storage/test-data.php | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/plugins/optimization-detective/storage/data.php b/plugins/optimization-detective/storage/data.php index ccf508d793..36d7c7c84f 100644 --- a/plugins/optimization-detective/storage/data.php +++ b/plugins/optimization-detective/storage/data.php @@ -212,7 +212,11 @@ static function ( $post ): ?array { 'post_modified_gmt' => $post->post_modified_gmt, ); }, - ( $wp_query instanceof WP_Query && $wp_query->post_count > 0 ) ? $wp_query->posts : array() + ( + $wp_query instanceof WP_Query && + is_array( $wp_query->posts ) && + count( $wp_query->posts ) > 0 + ) ? $wp_query->posts : array() ) ), 'active_theme' => array( diff --git a/plugins/optimization-detective/tests/storage/test-data.php b/plugins/optimization-detective/tests/storage/test-data.php index 180ae7ea97..6bdcd3dbb0 100644 --- a/plugins/optimization-detective/tests/storage/test-data.php +++ b/plugins/optimization-detective/tests/storage/test-data.php @@ -506,6 +506,16 @@ static function ( $link, $author_id ) { }; }, ), + + 'null_wp_query_posts' => array( + 'set_up' => function (): void { + $post = self::factory()->post->create_and_get(); + $this->assertInstanceOf( WP_Post::class, $post ); + $this->go_to( '/' ); + global $wp_query; + unset( $wp_query->posts ); + }, + ), ); } From 5bd607bc2d80f1df278c9158caad6e45bbadcb76 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 13 Jan 2026 23:48:07 -0800 Subject: [PATCH 2/2] Remove unnecessary condition --- plugins/optimization-detective/storage/data.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/plugins/optimization-detective/storage/data.php b/plugins/optimization-detective/storage/data.php index 36d7c7c84f..336e7f3204 100644 --- a/plugins/optimization-detective/storage/data.php +++ b/plugins/optimization-detective/storage/data.php @@ -212,11 +212,7 @@ static function ( $post ): ?array { 'post_modified_gmt' => $post->post_modified_gmt, ); }, - ( - $wp_query instanceof WP_Query && - is_array( $wp_query->posts ) && - count( $wp_query->posts ) > 0 - ) ? $wp_query->posts : array() + ( $wp_query instanceof WP_Query && is_array( $wp_query->posts ) ) ? $wp_query->posts : array() ) ), 'active_theme' => array(