Route subpage to Custom Post Type

I usually struggle with this, however this time I managed it relatively easy. Main reference for this answer is here:

https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

I’d like to use anything in the below as discipline.

/artists/djave_co/anything

First of all, I added a “rewrite tag”

function custom_rewrite_tag() {
  add_rewrite_tag('%discipline%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);

Next, I did print_r($wp_query->query_vars) on the default custom post page. This resulted in the below:

Array
(
    [page] => 0
    [artists] => djave_co
    [post_type] => artists
    [name] => djave_co
    [error] => 
    ...

This helped me, as I could see that I’d need to set post_type=artists&artists=djave_co in the next step.

The next step was to add a custom rewrite rule:

function custom_rewrite_rule() {
  add_rewrite_rule(
    '^artists/([^/]*)/([^/]*)/?',
    'index.php?post_type=artists&artists=$matches[1]&discipline=$matches[2]',
    'top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);

The final and very important step was to clear the permalinks in the WordPress admin panel.