WordPress Nested Rewrite endpoints

I am developing a WordPress ecommerce plugin.
I am using a custom database table to store products, so to access them in front-end I need to use custom endpoints.
Adding simple rewrite endpoints works fine, but I wonder how I can add nested rewrite endpoints.

function mycustom_add_endpoint() {
    add_rewrite_endpoint( 'my-products', EP_ROOT );
add_action( 'init', 'mycustom_add_endpoint' );

function mycustom_endpoints_activate() {
    // ensure our endpoint is added before flushing rewrite rules
    // flush rewrite rules - only do this on activation as anything more frequent is bad!
register_activation_hook( __FILE__, 'mycustom_endpoints_activate' );

function mycustom_template_redirect() {
    global $wp_query;

    // if this is not a request for my-products or a singular object then bail
    if ( ! isset( $wp_query->query_vars['my-products'] ))

    // include custom template
    include dirname( __FILE__ ) . '/template.php';
add_action( 'template_redirect', 'mycustom_template_redirect' );

The code above adds http://example.com/my-products endpoint so I can access single products http://example.com/my-products/product-name.

Now I need to somehow construct a logic to access product tags via http://example.com/my-products/tags/tag-name URL.
Thanks in advance.
With best regards,

Read more here: WordPress Nested Rewrite endpoints

Leave a Reply

Your email address will not be published. Required fields are marked *