WordPress.org

Make WordPress Core

Opened 2 months ago

Closed 2 months ago

#49024 closed defect (bug) (duplicate)

Notice: Trying to get property XX of non-object in WP_Query Class

Reported by: agengineering Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.3.1
Component: Query Keywords:
Focuses: Cc:
PR Number:

Description

Hi there,

sometimes the function

is_page()

return some notice like this:

Notice: Trying to get property ‘ID’ of non-object in /home/talabiah/public_html/wp-includes/class-wp-query.php on line 3965

Notice: Trying to get property ‘post_title’ of non-object in /home/talabiah/public_html/wp-includes/class-wp-query.php on line 3967

The reason is that the queried object is null and we use it without any kind of check.
Look here:

  1. First step: call function is_page():


wp-includes/query.php:566

  1. is_page call the method is_page() of WP_Query class, look the file wp-includes/class-wp-query.php:3952
WP_Query::is_page() call WP_Query::get_queried_object()


  1. get_queired_object() method hae a set of if-elseif but we are not sure that the default value:
$this->queried_object    = null;

will be change. There other functions like get_post() or get_userdata() that can return null/false value.

This value will return to WP_Query::is_page()


wp-includes/class-wp-query.php:3427

  1. In WP_Query::is_page() there aren't any check if this value is an object, but we use it like if we are sure that the get_queried_object() can return ONLY an object.


I think the best solution is to check if $page_obj is empty or not. A solution can be this:

<?php
        /**
         * Is the query for an existing single page?
         *
         * If the $page parameter is specified, this function will additionally
         * check if the query is for one of the pages specified.
         *
         * @see WP_Query::is_single()
         * @see WP_Query::is_singular()
         *
         * @since 3.1.0
         *
         * @param int|string|array $page Optional. Page ID, title, slug, path, or array of such. Default empty.
         * @return bool Whether the query is for an existing single page.
         */
        public function is_page( $page = '' ) {
                if ( ! $this->is_page ) {
                        return false;
                }

                if ( empty( $page ) ) {
                        return true;
                }

                $page_obj = $this->get_queried_object();

                $page = array_map( 'strval', (array) $page );

                if ( ! empty( $page_obj ) && in_array( (string) $page_obj->ID, $page ) ) {
                        return true;
                } elseif ( ! empty( $page_obj ) && in_array( $page_obj->post_title, $page ) ) {
                        return true;
                } elseif ( ! empty( $page_obj ) && in_array( $page_obj->post_name, $page ) ) {
                        return true;
                } else {
                        foreach ( $page as $pagepath ) {
                                if ( ! strpos( $pagepath, '/' ) ) {
                                        continue;
                                }
                                $pagepath_obj = get_page_by_path( $pagepath );

                                if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) {
                                        return true;
                                }
                        }
                }

                return false;
        }
?>

Thanks for your attention.

Pleae note: I have found a similar reporting here: #29660

Attachments (4)

01.png (12.0 KB) - added by agengineering 2 months ago.
02.png (31.3 KB) - added by agengineering 2 months ago.
03.png (49.1 KB) - added by agengineering 2 months ago.
04.png (38.8 KB) - added by agengineering 2 months ago.

Download all attachments as: .zip

Change History (6)

@agengineering
2 months ago

@agengineering
2 months ago

@agengineering
2 months ago

@agengineering
2 months ago

#1 @agengineering
2 months ago

  • Component changed from General to Query
  • Version set to 5.3.1

#2 @SergeyBiryukov
2 months ago

  • Focuses coding-standards removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi there, welcome back to WordPress Trac!

Thanks for the report, we're already tracking this issue in #29660.

Note: See TracTickets for help on using tickets.