Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#50925 closed defect (bug) (duplicate)

Function is_page() produce error on the front pages

Reported by: ivijanstefan's profile ivijanstefan Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.4.2
Component: Query Keywords: has-patch
Focuses: Cc:

Description

The is_page() function suddenly creates a problem on certain installations because it does not find the ID, post_title, and post_name objects.

Notice: Trying to get property 'ID' of non-object in /home/wp-includes/class-wp-query.php on line 3996

Notice: Trying to get property 'post_title' of non-object in /home/wp-includes/class-wp-query.php on line 3998

Notice: Trying to get property 'post_name' of non-object in /home/wp-includes/class-wp-query.php on line 4000

This means that the object from get_queried_object() was NULL.

It would be good to fix it like this:

<?php
public function is_page( $page = '' ) {
        if ( ! $this->is_page ) {
                return false;
        }

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

        $page_obj = $this->get_queried_object();

        if(!$page_obj) return false; /*** FIX ***/

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

        if ( in_array( (string) $page_obj->ID, $page ) ) {
                return true;
        } elseif ( in_array( $page_obj->post_title, $page ) ) {
                return true;
        } elseif ( 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;
}

This problem is only shown on the front page, while all other pages are working normally.

Change History (1)

#1 @SergeyBiryukov
4 years ago

  • Component changed from General to Query
  • 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.