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 )  {
  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?

1 Answer 1


Do no modify the queried_object property, instead use the set methods to modify the query to achieve your desired output. Below example will work. Use only one of the query, page_id(when you are setting a page id) or post__in/p (when you are setting post_id's array or a post_id respectively).

function replaceQuery( $query )  {
   if ( !is_admin() && $query->is_main_query() && $query->is_page ) {
     $childQuery = get_post(13);

     //to set/query multiple post ids
     $query->set( 'post__in', array( $childQuery->ID ) );

     //to set/query only a single post id
     $query->set( 'p', $childQuery->ID );

     //to set/query page id
     $query->set( 'page_id', $childQuery->ID );


add_action( 'pre_get_posts', 'replaceQuery');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.