Site icon Hip-Hop Website Design and Development

Add Rewrite Endpoint to CPT Archive

I have a Custom Post Type with a hierarchical taxonomy archive and want to add an endpoint page called "infopage" to each term in the archive.

e.g.

domain.com/tax-term-1/infopage

or

domain.com/tax-term-2/child-term-3/child-term-4/infopage

etc.

I don’t want to add this as a child term, but rather want it to be a "virtual" page as the contents will be generated dynamically.

Tried something like this but was getting a 404 when accessing the endpoint page (yes, I flushed permalinks):

add_action( 'init', function() {
    add_rewrite_endpoint( 'infopage', EP_PAGES );
} );

add_action( 'template_redirect', function() {
    global $wp_query;
    if ( ! isset( $wp_query->query_vars['infopage'] ) ) {
        return;
    }
    include plugin_dir_path( __FILE__ ) . 'templates/infopage.php';
    die;
} );

Is add_rewrite_endpoint not meant to be used with CPTs and custom taxonomies? If not, is there a recommended alternative way to achieve what I need?