Thanks to Sally CJ, here is the solution that worked for me :
/*To create new custom taxonomy */
function si0b_ct_year()
{
/* Property Type */
$labels = array(
'name' => _x('Years', 'Taxonomy General Name', 'siobanone'),
'singular_name' => _x('Year', 'Taxonomy Singular Name', 'siobanone')
);
$rewrite = array(
'slug' => 'artistes',
'with_front' => false,
'hierarchical' => true,
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'rewrite' => $rewrite,
'show_in_rest' => true,
);
register_taxonomy('aec-year', array('artists'), $args);
}
add_action('init', 'si0b_ct_year', 10);
/*To create new custom post type */
function si0b_cpt_artists()
{
// CPT : artists
$labels = array(
'name' => _x('Artists', 'Post Type General Name', 'siobanone'),
'singular_name' => _x('Artist', 'Post Type Singular Name', 'siobanone'),
);
$rewrite = array(
'slug' => 'artistes',
'with_front' => false,
);
$args = array(
'label' => __('artists', 'siobanone'),
'description' => __('Artists', 'siobanone'),
'labels' => $labels,
'taxonomies' => array('aec-year', 'post_tag'),
'hierarchical' => true,
'public' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'query_var' => 'artists',
'rewrite' => $rewrite,
'show_in_rest' => true,
);
register_post_type('artists', $args);
}
add_action('init', 'si0b_cpt_artists', 10);
/**Allow this :
* www.mysite.com/sioban_cpt/foo/ => shows all posts with the foo taxonomy
* www.mysite.com/sioban_cpt/bar => shows the indivual post under the sioban_cpt post-type
**/
function si0b_parse_request( $wp ) {
$path="artistes"; // rewrite slug; no trailing slashes
$taxonomy = 'aec-year'; // taxonomy slug
$post_type="artists"; // post type slug
if ( preg_match( '#^' . preg_quote( $path, '#' ) . '/#', $wp->request ) &&
isset( $wp->query_vars[ $taxonomy ] ) ) {
$slug = $wp->query_vars[ $taxonomy ];
$slug = ltrim( substr( $slug, strrpos( $slug, "https://wordpress.stackexchange.com/" ) ), "https://wordpress.stackexchange.com/" );
if ( ! term_exists( $slug, $taxonomy ) ) {
$wp->query_vars['name'] = $wp->query_vars[ $taxonomy ];
$wp->query_vars['post_type'] = $post_type;
$wp->query_vars[ $post_type ] = $wp->query_vars[ $taxonomy ];
unset( $wp->query_vars[ $taxonomy ] );
}
}
}
add_action( 'parse_request', 'si0b_parse_request' );