Avatar of jblayney
Flag for Canada

asked on 

Notice: Trying to get property 'ID' of non-object in WordPress


I have a function running in WordPress which is causing the above Notice. The function works, but I want the Notice to go away. The Notice only appears on the home page. This is the delinquent code  $query->queried_object->ID. I can't seem to declare the object

 function isu_search_url( $query ) {
            $page_id = 5554; 
            $per_page = 5;
            if ( !is_admin() && $query->is_main_query() && $query->queried_object->ID == $page_id  ) {
                add_filter( 'body_class', function( $classes ) {
                    $classes[] = 'filter-search';
                    return $classes;
                } );
                    $query->set( 'pagename', '' ); // we reset this one to empty!
                    $query->set( 'posts_per_page', $per_page ); 
                    $query->is_search = true; // We making WP think it is Search page 
                    $query->is_page = false; // disable unnecessary WP condition
                    $query->is_singular = false; // disable unnecessary WP condition
        add_action( 'pre_get_posts', 'isu_search_url' );

Open in new window


Avatar of undefined
Last Comment
David H.H.Lee

8/22/2022 - Mon