Site icon Hip-Hop Website Design and Development

How to use template_include hook with form submission?

I’m writing a plugin that uses the template_include hook to use a custom template for a custom post type. This works with GET requests, but with POST requests, it 404s because the $post variable is null within this hook in POST requests. How do I fix this so that I can use this custom template for both GET and POST requests?

namespace mynamespace;

class MyPlugin {

    public static function template_include($template) {
        global $post;
        var_dump($post); //$post exists for GET requests but is null for POST requests.
        var_dump(get_queried_object()); 
        // Same with get_queried_object.  
        // So for POST requests, I have no way of telling if this is the page of the specific 
        // custom post type that I want to target here.
        if ($post and $post->post_type == 'thing') { // true for GET, false for POST
            return plugin_dir_path( __FILE__ ) . 'templates/thing.php';
        }
        else if (get_query_var('post_type') == 'thing') { // true for GET, false for POST
            return plugin_dir_path( __FILE__ ) . 'templates/thing.php';
        }
        return $template;
    }

}

add_filter('template_include', 'mynamespaceMyPlugin::template_include');