I can't seem to get $query->queried_object->ID on homepage of WordPress, Notice: Trying to get property 'ID' of non-object:
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' );
source https://stackoverflow.com/questions/68535141/i-cant-seem-to-get-query-queried-object-id-on-homepage-of-wordpress-notice
Comments
Post a Comment