Specific routing for CPT

I’m developing site for art gallery. A have Artist CPT (internal name artsin_artist) which is defined in my functions.php like following:

register_post_type('artsin_artist', array(
    'labels' => $labels,
    'public' => true,
        'hierarchical' => false,
        'supports' => array('title'),
        'has_archive' => true,
        'rewrite' => array('with_front' => true, 'slug' => 'artists'),
        'publicly_queryable' => true,
));

So i have good working archive http://artsindika.ru/artists/ and artist’s page http://artsindika.ru/artists/arthur-ter-martirosov/ .

Read More

Every artist has some number of works, works are not dedicated post type, but it is a simple associative array (i have used AdvancedCustomFields Repeater plugin for this feature).

Now I need to have a page with following url structure artists/%artists_cpt_slug%/%work_index%.

For example for second work of Arthur Ter-Martirosov i need to have following url structure: /artists/arthur-ter-martirosov/2.

I tried to use a lot of recomendations about working with WordPress rewrites, but i always have archive page as a result of routing.

I mean that i need work_index query variable in my template to show approriate work. Now i am using following code for my rewrites:

function add_artists_query_vars($aVars) {
    $aVars[] = "work_index"; 
    return $aVars;
}

add_filter('query_vars', 'add_artists_query_vars');


function add_artists_rewrite_rule($aRules) {
    $aNewRules = array('artists/([^/]*)/([0-9]+)?$' => 'index.php?post_type=artsin_artist&post_name=$matches[1]&work_index=$matches[2]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

add_filter('rewrite_rules_array', 'add_artists_rewrite_rule');

Please tell me what I’m missing and what direction I need to move on?

Related posts