Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter dakta

    (@dakta)

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

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Cunstom Taxonomy Archives with Custom Post Types’ is closed to new replies.