1

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?

1 Answer 1

1

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

2
  • 1
    This worked perfectly! Thanks!
    – Edegist
    Commented Jan 30, 2022 at 1:42
  • PS: Sorry, I meant to use foo_cat and not foo_cpt as the taxonomy slug..
    – Sally CJ
    Commented Jan 30, 2022 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.