[resolved] Cunstom Taxonomy Archives with Custom Post Types (3 posts)

  1. dakta
    Posted 2 years ago #

    I have a custom post type with a custom taxonomy. The custom taxonomy is only registered with that custom post type.

    I have moved the posts index (the home page) to http://example.com/blog/ by setting the "Posts page" in Settings->Reading and prepending "/blog/" to the "Custom Structure" on Settings->Permalinks. This works well for blog posts, categories, and tags. I get http://example.com/blog/%year%/%monthnum%/%postname%/ permalinks, which I want, and my custom post type gets http://example.com/custom-type/%postname%/.

    However, there is a problem: I want to have custom taxonomy archive at http://example.com/custom-type/taxonomy-name/%taxonomy%/ but I can't figure it out. My Custom Taxonomy gets permalinks and a functional archive page at http://example.com/taxonomy-name/%taxonomy%/. But if I change slug in the custom taxonomy rewrite parameters to custom-type/taxonomy-name, I cannot access the taxonomy at either http://example.com/custom-type/taxonomy-name/%taxonomy%/ or http://example.com/taxonomy-name/%taxonomy%/

    And yes, I've flushed my permalinks settings.

    Is this the proper way to do this?

  2. dakta
    Posted 2 years ago #

    OK, so I have found a solution that doesn't require writing a custom rewriting filter. Please refer to this StackOverflow answer: http://wordpress.stackexchange.com/a/57515/44750

    The key is this:

    Change the has_archive parameter of the custom post type from true to the value of the custom post type's slug. This allows us to hijack the rewrite parameter to create the custom taxonomy rewrite rule and preserve proper rewriting of the custom post type archive. Next, change the value of the rewrite slug like this: 'slug' => 'custom_post_type_slug/%taxonomy_slug% where custom_post_type_slug is the same as the value you set for has_archive and taxonomy_slug is the same as the slug of the custom taxonomy. Keep the % characters.

    Important note: you must have a rewrite set for the custom taxonomy and for the custom post type, with 'with_front' => false. Here's an example with the important parts marked:

    // Register Custom Taxonomy
    function example_taxonomy_init()  {
    	$labels = array(
    		'name'                       => 'Example Taxonomy',
    		'singular_name'              => 'Example Taxonomy',
    		'menu_name'                  => 'Example Taxonomy',
    		'all_items'                  => 'All Example Taxonomies',
    		'parent_item'                => 'Parent Example Taxonomy',
    		'parent_item_colon'          => 'Parent Example Taxonomy:',
    		'new_item_name'              => 'New Example Taxonomy Name',
    		'add_new_item'               => 'Add New Example Taxonomy',
    		'edit_item'                  => 'Edit Example Taxonomy',
    		'update_item'                => 'Update Example Taxonomy',
    		'separate_items_with_commas' => 'Separate example taxonomies with commas',
    		'search_items'               => 'Search example taxonomies',
    		'add_or_remove_items'        => 'Add or remove example taxonomies',
    		'choose_from_most_used'      => 'Choose from the most used example taxonomies',
            // IMPORTANT!
    	$rewrite = array(
    	    'slug' => 'example-custom-post/example-taxonomy',
    	    'with_front' => false,
    	$args = array(
    		'labels'                     => $labels,
    		'hierarchical'               => true,
    		'public'                     => true,
    		'show_ui'                    => true,
    		'show_admin_column'          => true,
    		'show_in_nav_menus'          => true,
    		'show_tagcloud'              => true,
    		'rewrite'                    => $rewrite,
    	register_taxonomy( 'example-taxonomy', 'example-custom-post', $args );
    // Hook into the 'init' action
    add_action( 'init', 'example_taxonomy_init', 0 );</p>
    <p>// Register Custom Post Type
    function example_custom_post_init() {
    	$labels = array(
    		'name'                => 'Example Custom Posts',
    		'singular_name'       => 'Example Custom Post',
    		'menu_name'           => 'Example Custom Posts',
    		'parent_item_colon'   => 'Example Custom Post:',
    		'all_items'           => 'All Example Custom Posts',
    		'view_item'           => 'View Example Custom Post',
    		'add_new_item'        => 'Add New Example Custom Post',
    		'add_new'             => 'New Example Custom Post',
    		'edit_item'           => 'Edit Example Custom Post',
    		'update_item'         => 'Update Example Custom Post',
    		'search_items'        => 'Search example custom posts',
    		'not_found'           => 'No example custom posts found',
    		'not_found_in_trash'  => 'No example custom posts found in Trash'
            // IMPORTANT!
    	$rewrite = array(
    		'slug'                => 'example-custom-posts/%example-taxonomy%', // keep the % characters
    		'with_front'          => false,
    	$args = array(
    		'label'               => 'example-custom-post',
    		'description'         => 'An example custom post type',
    		'labels'              => $labels,
    		'supports'            => array( 'title', 'editor', 'excerpt', 'thumbnail', ),
    		'taxonomies'          => array( 'example_taxonomy' ), // link to our custom taxonomy
    		'hierarchical'        => false,
    		'public'              => true,
    		'show_ui'             => true,
    		'show_in_menu'        => true,
    		'show_in_nav_menus'   => true,
    		'show_in_admin_bar'   => true,
    		'menu_position'       => 5,
    		'menu_icon'           => '',
    		'can_export'          => true,
    		'has_archive'         => 'example-custom-posts',
    		'exclude_from_search' => false,
    		'publicly_queryable'  => true,
    		'capability_type'     => 'page',
    		'rewrite'             => $rewrite
    	register_post_type( 'example-custom-post', $args );
    // Hook into the 'init' action
    add_action( 'init', 'example_custom_post_init', 0 );
  3. jphase
    Posted 2 years ago #

    Thanks dakta - there are many out there that have been trying to google this specific need without an explicit answer. Your solution was quite helpful.

Topic Closed

This topic has been closed to new replies.

About this Topic