The good answer was from @Milo, but he haven’t posted any code, so I post here my result, for people coming here for the right answer.
/* Add query var to authorized query var... */
add_filter( 'query_vars', 'add_spectacles_query_vars' );
function add_spectacles_query_vars( $query_vars ) {
$query_vars[] = 'spectacles-year';
$query_vars[] = 'spectacles-month';
$query_vars[] = 'spectacles-day';
return $query_vars;
}
/* Add rewrite rule */
add_action( 'init', 'add_spectacles_rewrite_rule' );
function add_spectacles_rewrite_rule()
{
add_rewrite_rule(
'spectacles\/(on){1}\/{1}(\d{4})+\/?(\d{2})?\/?(\d{2}|d{1})?',
'index.php?post_type=programmation&spectacles-year=$matches[2]&spectacles-month=$matches[3]&spectacles-day=$matches[4]',
'top'
);
//flush_rewrite_rules();
}
Than in my template I can go with
$spectacle_year = get_query_var('spectacles-year');
$spectacle_month = get_query_var('spectacles-month');
$spectacle_day = get_query_var('spectacles-day');
P.S. I’m filtering my spectacles directly in the archive page, but it could be done via :
add_action( 'pre_get_posts', 'spectacles_archvies_pre_get_posts', 11 );
function spectacles_archvies_pre_get_posts($query) {
if( !empty($query->query_vars['spectacles-year']) ) {
/* Do your stuff here... */
}
}