Add Rewrite Endpoint to CPT Archive

You’re getting the 404 most likely because your custom taxonomy is not assigned to the endpoint mask (EP_PAGES) used with your endpoint.

Because add_rewrite_endpoint() does work with custom post types and taxonomies, but you need to assign the endpoint mask to the post type or taxonomy during registration, via the ep_mask key in the rewrite argument for register_taxonomy() as well as register_post_type(), like so:

// Add the "infopage" endpoint with the endpoint mask EP_PAGES.
add_rewrite_endpoint( 'infopage', EP_PAGES );

// Register a "foo" post type.
register_post_type( 'foo', array(
    'public'  => true,
    'label'   => 'Foos',
    'rewrite' => array(
        'ep_mask' => EP_PAGES, // assign EP_PAGES to the CPT
        'slug'    => 'foos',
    ),
) );

// Register a hierarchical taxonomy for the "foo" CPT above.
register_taxonomy( 'foo_cpt', 'foo', array(
    'public'       => true,
    'label'        => 'Foo Categories',
    'hierarchical' => true,
    'rewrite'      => array(
        'ep_mask'      => EP_PAGES, // assign EP_PAGES to the taxonomy
        'hierarchical' => true,     // this one is entirely up to you
    ),
) );

So assign the endpoint mask and then flush the rewrite rules (simply visit the Permalink Settings admin page), and see if you’re still getting the 404.


Additionally, if you’re unable to change the register_taxonomy() or register_post_type() code (e.g. because the post type or taxonomy is registered using a 3rd-party plugin), then you could use the register_taxonomy_args or register_post_type_args hook to modify the taxonomy or post type arguments.

And as for the “recommended alternative”, then it would be using add_rewrite_rule() to manually add the rewrite rule for your endpoint, e.g. add_rewrite_rule( 'foo_cpt/(.+?)/infopage(/(.*))?/?$', 'index.php?foo_cpt=$1&infopage=$3', 'top' ) for the foo_cpt taxonomy above. 🙂