Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

A few issues-

  • there is no %book_type% rewrite tag, your rewrite tags are %book% and %chapter%. Change all instances of %book_type% to %book%.

  • Your taxonomy slug should just be 'rewrite' => array( 'slug' => 'books'), the rewrite tag shouldn't be in there.

  • Last (minor) issue- In your post_type_link filter, you should wrap the fetching of the book term in an if check so you only try to replace the tag if a term is selected. otherwise, with debugging enabled you'll see a trying to get property of non-object notice from trying to get the slug of a non-existent term.

    if( $book_type_term = wp_get_object_terms( $post->ID, 'book' ) )
        $post_link = str_replace( '%book%', array_pop( $book_type_term )->slug, $post_link );
    

tested and working in twentythirteen theme- visit your permalinks settings page after making changes, to flush rewrite rules, and all should work as expected.

share|improve this answer
    
Correct me if I'm wrong but you say there isn't a %book_type% rewrite tag, however if I'm correct, isn't the rewrite tag "defined" in my post_type_link filter through the str_replace function? Why would changing from %book_type% to %book% help? I use the same setup with "type" appended to it for another CPT on the same blog and it works as expected. Not sure why that isn't the case for this one. –  Thought Space Designs Sep 13 '13 at 3:50
    
your rewrite tags are defined when you register your post type and taxonomy, they're used by WordPress to convert a request into the necessary query vars to query the database. you can see them exposed on the front end if you set permalinks to default, disabling pretty permalinks, then you'll have URLs like /?book=some-book or /?chapter=a-chapter –  Milo Sep 13 '13 at 4:01
    
So you're saying that rewrite tags are by default automatically created to match the post type query var key? –  Thought Space Designs Sep 13 '13 at 4:13
    
yes, that's correct. –  Milo Sep 13 '13 at 4:17
    
I'll check this tomorrow and see if it solves my issue. Thanks for the info. –  Thought Space Designs Sep 13 '13 at 5:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.