Skip to main content
2 of 3
edited title

queried_object using pre_get_posts gets notices and warning

My code is getting me the result I want on my page, I'm fetching another post and my theme is outputting all of the relevant data I need on the page as expected.

 function replaceQuery( $query )
{
  
  // meta_query date_query queried_object request  queried_object_id posts found_posts
    if ( !is_admin() && $query->is_main_query() && $query->is_page  ) 
    {

    


        $childQuery = get_post(13);

        
        
        $query->queried_object->ID = $childQuery->ID;
        $query->queried_object->post_content = $childQuery->post_content;
        $query->queried_object->post_title = $childQuery->post_title;
        $query->queried_object->post_name = $childQuery->post_name;
        $query->queried_object->queried_object_id = $childQuery->post_name;
        $query->query_vars['page_id'] = $childQuery->ID;
        $query->query_vars['p'] = $childQuery->ID;
        

    }
}
// I've also used the "parse_query" hook
add_action( 'pre_get_posts', 'replaceQuery');

The trouble is, I'm getting warnings and notices for everything

Notice:  Indirect modification of overloaded property WP_Query::$queried_object has no effect in mycode.php on line 47

Warning:  Creating default object from empty value in mycode.php on line 47

I found a similar issue here is_category in pre_get_posts results in php notices

It seems like I'm trying to make my changes to the WP Query object either too early or (in the case above) too late. The properties I've tried using as conditionals have eliminated many errors, but when I add anything else, it seems to break the code.

Is there another conditional I can use or perhaps another hook to get rid of these notices & warnings?