From c11ce53dceee9d8af402f16af23ab4aa61e6a074 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 11 Oct 2022 18:15:13 +0000 Subject: [PATCH] Query: Avoid PHP notices when `get_queried_object()` returns `null`. `WP_Query` methods assume that `get_queried_object()` would return a non-null value, which is not always the case. This commit resolves various warnings in `WP_Query` along the lines of: {{{ Attempt to read property "post_type" on null in wp-includes/class-wp-query.php on line 4338 }}} Follow-up to [1728], [3639], [8807], [49119]. Props dd32, yellyc, boonebgorges, darkskipper, Howdy_McGee, swissspidy, nacin, mikeschroder, mikejolley, sterlo, datainterlock, utsavmadaan823, kanlukasz, woji29911, hellofromTonya, zikubd, deksar, bwbama, noplanman, nouarah, SergeyBiryukov. Fixes #29660. Built from https://develop.svn.wordpress.org/trunk@54496 git-svn-id: http://core.svn.wordpress.org/trunk@54055 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-query.php | 21 +++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php index e48d942f0c..1f6f926b1a 100644 --- a/wp-includes/class-wp-query.php +++ b/wp-includes/class-wp-query.php @@ -3963,6 +3963,9 @@ class WP_Query { $attachment = array_map( 'strval', (array) $attachment ); $post_obj = $this->get_queried_object(); + if ( ! $post_obj ) { + return false; + } if ( in_array( (string) $post_obj->ID, $attachment, true ) ) { return true; @@ -3996,6 +3999,9 @@ class WP_Query { } $author_obj = $this->get_queried_object(); + if ( ! $author_obj ) { + return false; + } $author = array_map( 'strval', (array) $author ); @@ -4032,6 +4038,9 @@ class WP_Query { } $cat_obj = $this->get_queried_object(); + if ( ! $cat_obj ) { + return false; + } $category = array_map( 'strval', (array) $category ); @@ -4068,6 +4077,9 @@ class WP_Query { } $tag_obj = $this->get_queried_object(); + if ( ! $tag_obj ) { + return false; + } $tag = array_map( 'strval', (array) $tag ); @@ -4315,6 +4327,9 @@ class WP_Query { } $page_obj = $this->get_queried_object(); + if ( ! $page_obj ) { + return false; + } $page = array_map( 'strval', (array) $page ); @@ -4422,6 +4437,9 @@ class WP_Query { } $post_obj = $this->get_queried_object(); + if ( ! $post_obj ) { + return false; + } $post = array_map( 'strval', (array) $post ); @@ -4469,6 +4487,9 @@ class WP_Query { } $post_obj = $this->get_queried_object(); + if ( ! $post_obj ) { + return false; + } return in_array( $post_obj->post_type, (array) $post_types, true ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 2fef87b0df..ddef1a537b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-beta3-54495'; +$wp_version = '6.1-beta3-54496'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.