Make WordPress Core


Ignore:
Timestamp:
10/11/2022 06:13:34 PM (4 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r54464 r54496  
    39643964
    39653965                $post_obj = $this->get_queried_object();
     3966                if ( ! $post_obj ) {
     3967                        return false;
     3968                }
    39663969
    39673970                if ( in_array( (string) $post_obj->ID, $attachment, true ) ) {
    … …  
    39974000
    39984001                $author_obj = $this->get_queried_object();
     4002                if ( ! $author_obj ) {
     4003                        return false;
     4004                }
    39994005
    40004006                $author = array_map( 'strval', (array) $author );
    … …  
    40334039
    40344040                $cat_obj = $this->get_queried_object();
     4041                if ( ! $cat_obj ) {
     4042                        return false;
     4043                }
    40354044
    40364045                $category = array_map( 'strval', (array) $category );
    … …  
    40694078
    40704079                $tag_obj = $this->get_queried_object();
     4080                if ( ! $tag_obj ) {
     4081                        return false;
     4082                }
    40714083
    40724084                $tag = array_map( 'strval', (array) $tag );
    … …  
    43164328
    43174329                $page_obj = $this->get_queried_object();
     4330                if ( ! $page_obj ) {
     4331                        return false;
     4332                }
    43184333
    43194334                $page = array_map( 'strval', (array) $page );
    … …  
    44234438
    44244439                $post_obj = $this->get_queried_object();
     4440                if ( ! $post_obj ) {
     4441                        return false;
     4442                }
    44254443
    44264444                $post = array_map( 'strval', (array) $post );
    … …  
    44704488
    44714489                $post_obj = $this->get_queried_object();
     4490                if ( ! $post_obj ) {
     4491                        return false;
     4492                }
    44724493
    44734494                return in_array( $post_obj->post_type, (array) $post_types, true );
Note: See TracChangeset for help on using the changeset viewer.