1

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.

  • You're reusing an existing queryvar, if you visit site.com/genres/action it gets processed into index.php?genres=action, so site.com/books/?genre=action doesn't look any different to it. Is there a reason you don't just prefix the genres taxonomy with books/ so it has a books/genres/action URL? Then you can just use get_term_link and have nicer URLs, and the ability to have dedicated book genre archive templates? Even the filter would be easier to implement as you could just call one of the native WP functions like tag cloud or the dropdown? – Tom J Nowell Aug 1 at 11:09
  • Don't forget, you can add ?s= on the end of any archive to search it – Tom J Nowell Aug 1 at 11:10
  • Thanks Tom. I'am not prefixing with book and usigng ?s= because i want to crate a "global books browser" where i can search by multiple and different taxonomies at the same time and even combined with custom meta. All this in a single page. – yous Aug 1 at 11:50
  • Note that your custom meta should be a taxonomy if you intend to search or filter based on that data. Searching/Filtering for posts via their post meta is extremely expensive and gets slower as the number of posts increases, this is because normal queries can be super fast and rely on table indexes, but the post meta table was never built to make those kinds of queries fast. If it was, tags and categories would be post meta. That's why they built dedicated tables for filtering grouping and searching, aka taxonomies and terms. – Tom J Nowell Aug 1 at 11:54
  • Most field plugins such as ACF/Field manager/etc provide options for using a taxonomy instead of post meta to save the data, look up their documentation for how – Tom J Nowell Aug 1 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.