Support » Fixing WordPress » Permalink structure for custom post type archives filtered by taxonomy

  • Trying to come up with a solution to a permalink puzzle I’m working on. Hoping someone can see something I’m not or have some ideas.

    I’ve got a site We’ve got a custom post type ‘Books‘ with a slug of ‘books‘. One of the taxonomies associated with books is ‘Publisher‘ (using slug publisher). What I would like is to be able to list the books, filtered out by a specific publisher from the taxonomy. I can accomplish this right now by doing:

    But what I would really like is to have a pretty URL structure like:

    (which currently results in a 404). I thought I could accomplish this by using add_rewrite_endpoint() but that doesn’t seem to be working (and yes, I made sure to call flush_rewrite_rules()). I’ve also looked into post_link filter, but that seems to be a bit too specific. I’ve got several custom post types with taxonomies where I need to do the same thing, so what I’m really after is somehow setting up the entire site so that I can use<custom-post-type-slug>/<taxonomy-slug>/<tax-term-slug>

    And I want wordpress to match this request to the archive-$posttype.php template file.

    I’m confident this can’t be that difficult to accomplish and that I must be overlooking some piece of the documentation/API. Any ideas?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer


    Try to register the taxonomy with the rewrite parameter like this

    'rewrite'=> array( 'slug' => 'books/publisher' ),

    Re-save permalinks after editing the register_taxonomy arguments.

    Hhhhmmm… that will work for the taxonomies that are being used by only one custom post type, but I’ve got a couple of taxonomies that are used across several custom post types (and I apologize for not mentioning that in my initial post).

    Moderator keesiemeijer


    Register the taxonomy and your post types like this:

    This allows you to use the post type in the rewrite array for the taxonomy:

    add_rewrite_tag( '%post_type%', '([^/]+)' );

    rewrite array:

    'rewrite' => array(
        'slug'      => '/%post_type%/publisher',
        'walk_dirs' => false

    Unfortunately because the post type is now dynamic, core WordPress functions that output “publisher” term links will use an url with “%post_type%” in it. (e.g.

    With this in your theme’s functions.php you can remedy this somewhat:

    add_filter( 'term_link', 'term_link_filter', 10, 3 );
    function term_link_filter( $url, $term, $taxonomy ) {
    	if ( false !== strpos( $url, '%post_type%' ) ) {
    		$post_type = get_post_type();
    		if ( in_array( $post_type, array( 'books', 'movies' ) ) ) {
    			$url = str_replace( '%post_type%', $post_type, $url );
    	return $url;


    If you still want a solution for this I’ve written a plugin that does exactly what you want:


    Jonathandejong, I’ll give it a try and let you know.

    Hi there, i have the same (kind of) problem as gilzow, but my issue is that i am trying to do that for a custom post type and for the main taxonomy which i have renamed from categories to topics.

    For example if i try to get all posts that under a topic the page works fine. But if i try to get the news page filtered by a topic, for example i get a 404 page.
    I have created an archive-news.php page to handle the functionality, any ideas?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Permalink structure for custom post type archives filtered by taxonomy’ is closed to new replies.