Permalink structure for custom post type archives filtered by taxonomy (7 posts)

  1. gilzow
    Posted 2 years ago #

    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 abz.com. 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


    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?

  2. keesiemeijer
    Posted 2 years ago #

    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.

  3. gilzow
    Posted 2 years ago #

    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).

  4. keesiemeijer
    Posted 2 years ago #

    Register the taxonomy and your post types like this: http://pastebin.com/uhqAWyRi

    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. site.com/%post_type%/publisher/term-name).

    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;


  5. Jonathandejong
    Posted 1 year ago #


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


  6. gilzow
    Posted 1 year ago #

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

  7. tolisp
    Posted 1 year ago #

    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 abc.com/topics/topic1 works fine. But if i try to get the news page filtered by a topic, for example abc.com/news/topics/topic1 i get a 404 page.
    I have created an archive-news.php page to handle the functionality, any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic