So I'm currently working on a client site and trying to switch some old posts over to use new custom post types. Prior to this point, her website was set up in such a way that she could add both books with chapters as well as uncategorized advice columns to her site. In order to add either of these items, she would add a new post, and stick them in a specific category. If she wanted the post to show up as a chapter in her book, she filed it under the correct subcategory of the "books" category. If she wanted the post to show up in her advice columns, she would add it to the "advice" category.
Permalinks for this site were set up in such a way that when reading an individual chapter of a book, it would look like so "/books/the-book/the-chapter"
Now, I'm trying to recombobulate her set up a bit. Instead of simply adding a new post and categorizing it to achieve this effect, I split out the two different areas into their own custom post types (called chapter and column). When setting up the new custom post type, I'm trying to set up the rewrite argument to use the same permalink that was used previously, however it appears that even after deleting the category, the permalink is still reserved by the category. What I mean is I receive 404's when trying to access "/books/the-book/the-chapter", but if I change the rewrite to use "chapter", or another base, it works.
Mainly I'm wondering if there's a way to fix this, and what could be causing this. It has to do with the fact that the category still existed when the CPT was initially added, but I can't quite nail it down.
Here's the website where you can see it in action: http://dev1.abileneloving.com
Here's my code for the CPT/Custom Tax/Custom Permalink Structure:
function chapter_post_type_init() {
$labels = array(
'name' => 'Chapters',
'singular_name' => 'Chapter',
'add_new' => 'Add New',
'add_new_item' => 'Add New Chapter',
'edit_item' => 'Edit Chapter',
'new_item' => 'New Chapter',
'all_items' => 'All Chapters',
'view_item' => 'View Chapter',
'search_items' => 'Search Chapters',
'not_found' => 'No chapters found',
'not_found_in_trash' => 'No chapters found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Books'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'books/%book_type%'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 100,
'supports' => array( 'title', 'editor', 'author', 'excerpt')
);
register_post_type( 'chapter', $args );
}
add_action( 'init', 'chapter_post_type_init' );
function book_tax_init() {
$labels = array(
'name' => _x( 'Books', 'taxonomy general name' ),
'singular_name' => _x( 'Book', 'taxonomy singular name' ),
'search_items' => __( 'Search Books' ),
'all_items' => __( 'All Books' ),
'edit_item' => __( 'Edit Book' ),
'update_item' => __( 'Update Book' ),
'add_new_item' => __( 'Add New Book' ),
'new_item_name' => __( 'New Book Name' ),
'menu_name' => __( 'Books' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'books/%book_type%')
);
register_taxonomy(
'book',
'chapter',
$args
);
}
add_action( 'init', 'book_tax_init' );
add_filter('post_type_link', 'book_permalink_structure', 10, 4);
function book_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, '%book_type%' ) ) {
$book_type_term = wp_get_object_terms( $post->ID, 'book' );
$post_link = str_replace( '%book_type%', array_pop( $book_type_term )->slug, $post_link );
}
return $post_link;
}
Update - New Code
function chapter_post_type_init() {
$labels = array(
'name' => 'Chapters',
'singular_name' => 'Chapter',
'add_new' => 'Add New',
'add_new_item' => 'Add New Chapter',
'edit_item' => 'Edit Chapter',
'new_item' => 'New Chapter',
'all_items' => 'All Chapters',
'view_item' => 'View Chapter',
'search_items' => 'Search Chapters',
'not_found' => 'No chapters found',
'not_found_in_trash' => 'No chapters found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Books'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'books/%book%'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 100,
'supports' => array( 'title', 'editor', 'author', 'excerpt')
);
register_post_type( 'chapter', $args );
}
add_action( 'init', 'chapter_post_type_init' );
function book_tax_init() {
$labels = array(
'name' => _x( 'Books', 'taxonomy general name' ),
'singular_name' => _x( 'Book', 'taxonomy singular name' ),
'search_items' => __( 'Search Books' ),
'all_items' => __( 'All Books' ),
'edit_item' => __( 'Edit Book' ),
'update_item' => __( 'Update Book' ),
'add_new_item' => __( 'Add New Book' ),
'new_item_name' => __( 'New Book Name' ),
'menu_name' => __( 'Books' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'books')
);
register_taxonomy(
'book',
'chapter',
$args
);
}
add_action( 'init', 'book_tax_init' );
add_filter('post_type_link', 'book_permalink_structure', 10, 4);
function book_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, '%book%' ) ) {
$book_type_term = wp_get_object_terms( $post->ID, 'book' );
$post_link = str_replace( '%book%', array_pop( $book_type_term )->slug, $post_link );
}
return $post_link;
}