$page_id, 'post_type' => 'page', 'posts_per_page' => 1, // Used to bypass the restrictions and not add processing to the new query. 'ignore_restrictions' => true, ]; $custom_query = new \WP_Query( $args ); if ( ! $custom_query->have_posts() ) { return; } $query->init(); // Reset the main query. $query->query_vars = $args; // phpcs:disable:Squiz.PHP.CommentedOutCode.Found // $query->queried_object = $custom_query->post; // $query->queried_object_id = $page_id; // $query->post = $custom_query->post; // $query->posts = $custom_query->posts; // $query->query = $custom_query->query; // // Since init, only override defaults as needed to emulate page. // $query->is_page = true; // $query->is_singular = true; // $query->found_posts = 1; // $query->post_count = 1; // $query->max_num_pages = 1; // // Suppress filters. Might not need this. // $query->set( 'suppress_filters', true ); // phpcs:enable:Squiz.PHP.CommentedOutCode.Found // Ensure all query vars are set. $query->get_posts(); // Reset the post data. $query->reset_postdata(); }