WordPress.org

Forums

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

  1. gilzow
    Member
    Posted 9 months 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:

    abz.com/books/?publisher=tor

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

    abz.com/books/publisher/tor

    (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

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

  2. keesiemeijer
    moderator
    Posted 9 months ago #

    Try to register the taxonomy with the rewrite parameter like this

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

    http://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments

    Re-save permalinks after editing the register_taxonomy arguments.

  3. gilzow
    Member
    Posted 9 months 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
    moderator
    Posted 9 months 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;
    }

    https://core.trac.wordpress.org/ticket/14502

  5. Jonathandejong
    Member
    Posted 2 months ago #

    Hi,

    If you still want a solution for this I've written a plugin that does exactly what you want:
    https://wordpress.org/plugins/beautiful-taxonomy-filters/

    cheers

  6. gilzow
    Member
    Posted 2 months ago #

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

Reply

You must log in to post.

About this Topic