Need some help with some custom rewrite rules. I found this post How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name which I've tried to modify for my use and can't seem to get it working.

I have a PAGE with a custom template/query acting as the archive. I have a Custom Post Type and a non-hierarchical custom taxonomy as well. What I am trying to accomplish is this:

/basename/ (my page with a custom template)

/basename/category/ (actual taxonomy archive page)

/basename/category/post-name (custom post type single)

I've tried following the previous mentioned thread and simply adjusting the rewrite rules accordingly by changing the 4 to 3 since my post will be the 4th URI segment not the 5th, and also removing one of the (.+) as well. (although I'll be honest that rewrite code is greek to me.

With the following code the main page works, the taxonomy archive page works, but the single posts return 404. I've tried a bunch of different combinations. Anyone have any ideas?

function jc_dining() {
    $labels = array(
        'name' => 'Locations',
        'singular_name' => 'Location',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Location',
        'edit_item' => 'Edit Location',
        'new_item' => 'New Location',
        'all_items' => 'All Locations',
        'view_item' => 'View Location',
        'search_items' => 'Search Locations',
        'not_found' => 'No Locations found',
        'not_found_in_trash' => 'No Locations found in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'Dining'
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'menu_icon' => 'dashicons-carrot',
        'show_ui' => true,
        'query_var' => true,        
        'rewrite' => array( 'slug' => 'dining/%taxonomy_name%', 'with_front' => false  ),
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'has_archive' => false,
        'supports' => array('title', 'editor')
    register_post_type( 'dining_cpt' , $args );
add_action ('init', 'jc_dining', 0);
add_action ('init', 'create_dining_tax', 10);

// registering Category taxonomy for Dining
function create_dining_tax() {
    $labels = array(
        'name'              => 'Category',
        'singular_name'     => 'Category',
        'search_items'      => 'Search Categories',
        'all_items'         => 'All Categories',
        'parent_item'       => 'Parent Category',
        'parent_item_colon' => 'Parent Category:',
        'edit_item'         => 'Edit Category', 
        'update_item'       => 'Update Category',
        'add_new_item'      => 'Add New Category',
        'new_item_name'     => 'New Category',      
        'menu_name'         => 'Categories'
    $args = array(
        'labels' => $labels,
        'show_admin_column' => true,
        'hierarchical' => false,
        'rewrite' => array( 'slug' => 'dining', 'with_front' => false ),
        'show_ui' => true,          
        'query_var' => true,
        'public' => true        
    register_taxonomy( 'dining_cats', 'dining_cpt', $args );
// custom rewrite 
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
    $newRules  = array();
    $newRules['dining/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?dining_cpt=$matches[3]'; 
    $newRules['dining/(.+)/?$']                = 'index.php?dining_cats=$matches[1]'; 
    return array_merge($newRules, $rules);
// custom helper for permalinks
function wpse_5308_post_type_link( $link, $post ) {
    if ( $post->post_type === 'dining_cpt' ) {
        if ( $terms = get_the_terms( $post->ID, 'dining_cats' ) )
            $link = str_replace( '%taxonomy_name%', current( $terms )->slug, $link );
    return $link;
add_filter( 'post_type_link', 'wpse_5308_post_type_link', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.