Categories
CMS Mastering Development Wordpress

Use Custom Post Type archive page for the taxonomies term archive page

In my wordpress theme I created a new custom post type ‘books’ with 2 taxonomies (‘series’, ‘genres’). when I visit the archive of the cpt ‘books’ (site.com/books), I list all the books.

I added a custom frontend filter to get in this archive page books by taxonomy terms by passing an argument to the url with the name of the taxonomy and the terme (like this: site.com/books/?genres=action). Like a book browser.

But wordpress is by default creating the link for my taxonomies like this (site.com/genres/action/) and i want it to be "redirected" to the books post type with the taxonomy argument (site.com/books/?genres=action).

Is it possible to achieve that ? Thank you

Update

Actually I’m using this "workaround" :

// Change url link for term of taxonomies of CPT Book
function cpt_book_term_link_filter( $url, $term, $taxonomy ) {
    if ( in_array($taxonomy, get_object_taxonomies('books')) ) {
        $url = home_url() . '/books/?'. $taxonomy . '=' . $term->slug;
    }
    return $url;
}
add_filter( 'term_link', 'cpt_book_term_link_filter', 10, 3 );

With this I edit each taxonomies terms of the Custom Post Type book and change it to link to my Custom Post Type book archive’s page.

But I don’t think it’s the best solution. Because archives page for my taxonomies (books/genres/action, …) still exist and are used for canonical url in plugins like yoast seo. I can force to redirect them but I don’t think its good.

Leave a Reply

Your email address will not be published. Required fields are marked *