Manipulate query to show specific post

I want to alter the (main?) WP query so it will show a specific post under set circumstances. I tried:

add_action('pre_get_posts', 'a_b_c');

function a_b_c(&$query)
{
    //When in admin menu, ignore
    if (is_admin()) {
        return;
    }


    if ($someComplexConditionsHereThatWork == true) {

        if ($query->is_main_query()) {
           //query = new WP_Query(["p" => 7]); - Does not work..
           $post = get_post(7);
           $query->is_page = true;
           $query->is_singular = true;
           $query->is_home = false;
           $query->is_archive = false;
           $query->is_category = false;
           $query->query_vars['error'] = '';
           $query->is_404 = false;
           $query->current_post = $post->ID;
           $query->found_posts = 1;
           $query->post_count = 1;
           $query->comment_count = 0;
           $query->current_comment = null;
           $query->is_singular = 1;
           $query->post = $post;
           $query->posts = array($post);
           $query->queried_object = $post;
           $query->queried_object_id = $post->ID;
           $query->current_post = $post->ID;
           $query->post_count = 1;
        }

    }
}

With this code, the post title is shown correctly, but the content is empty.

Read more here: Manipulate query to show specific post

Leave a Reply

Your email address will not be published. Required fields are marked *