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?

Topic endpoints rewrite-rules Wordpress

Category Web


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. :)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.