Get url.com/post_type/taxonomy/term work!

I’m having trouble get what I want by mixing custom post types and taxonomy.

I want to run this:

Read More

www.url.com/post_type/taxonomy/term

In my case this:

www.url.com/dossier/theme/sante

(sante is a term of “thermatiques” taxonomy)

When I enter this url, I do not have a 404 but it shows me all the items post-type regardless of the taxonomy.

Taxonomy’s code:

register_taxonomy(
    'thematiques',
    array( 'post', 'dossiers','videos' ),
    array(
    'rewrite' => array( 'slug' => 'theme'),
    'label' => 'Thématiques',
    'query_var' => true,
    )
);

CPT’s code:

function register_cpt_dossiers() {
    $labels = array(
        'name' => _x( 'Dossiers', 'dossiers' ),
        'singular_name' => _x( 'Dossier', 'dossiers' ),
        'all_items' => _x( 'Tous les dossiers', 'dossiers' ),
        'add_new' => _x( 'Ajouter', 'dossiers' ),
        'add_new_item' => _x( 'Ajouter un nouveau dossier', 'dossiers' ),
        'edit_item' => _x( 'Modifier le dossier', 'dossiers' ),
        'new_item' => _x( 'Nouveau dossier', 'dossiers' ),
        'view_item' => _x( 'Afficher le dossier', 'dossiers' ),
        'search_items' => _x( 'Chercher dans les dossiers', 'dossiers' ),
        'not_found' => _x( 'Aucun dossier trouvé.', 'dossiers' ),
        'not_found_in_trash' => _x( 'Aucun dossier trouvé dans la corbeille.', 'dossiers' ),
        'parent_item_colon' => _x( 'Magazine parent :', 'dossiers' ),
        'menu_name' => _x( 'Dossiers', 'dossiers' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'revisions' ),
        'taxonomies' => array( 'thematiques'),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => 'dossier',
        'query_var' => true,
        'can_export' => true,
        'rewrite' => array( 'slug' => 'dossier/%thematiques%'),
        'capability_type' => 'post'
    );
    register_post_type( 'dossiers', $args );
}

And here is the code that redirects links posts correctly:

function wpa_show_permalinks( $post_link, $id = 0 ) {
    $post = get_post($id);
    if ( is_object( $post ) && $post->post_type == 'dossiers' ){
        $terms = wp_get_object_terms( $post->ID, 'thematiques' );
        if( $terms ){
            return str_replace( '%thematiques%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );

Thank you for your help!

Related posts