Child pages on hierarchical Custom Post Types 404s

I’ve been working with Custom Post Types lately and haven’t had any major problems. This time I’m trying something new. I want to create a CPT which basically works like Pages, with page hierarchy.

The real thing is in Swedish so I’ll use “Books” as an example on how I would like it to work.

So first I create my new post type:

define('BOOKS_HTTP_PATH', WP_PLUGIN_URL . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)));
function add_custom_post_type_books() {
    register_post_type( 'lpm-books', array( 
        'labels' => array( 
            'name' => 'Books',
            'singular_name' => 'Book',
            'add_new' => 'New book',
            'add_new_item' => 'Add new book',
            'not_found' => 'No books found' ),
        'public' => true, 
        'show_ui' => true,
        'show_in_nav_menus' => true,
        'menu_icon' => BOOKS_HTTP_PATH . 'images/books-icon.png',
        'menu_position' => 20,
        'hierarchical' => true,
        'rewrite' => array( 'slug' => 'books'),
        'capability_type' => 'page',
        'supports' => array('title','editor','page-attributes'),
        'query_var' => '',
        '_builtin' => false,
        ) 
    );
}
add_action( 'init', 'add_custom_post_type_books' );

In my case I have a specific name for the CPT, lpm-books (might sound weird in this example but as I said earlier I’m doing this in Swedish but I’d like the post type name in English if possible). I set the rewrite slug to books for my permalinks and I add 'hierarchical' => true and 'supports' => 'page-attributes' to make the Attribute meta box available.

In Admin->Books the view would be something like this:

  • Book 1
    • Chapter 1
    • Chapter 2
  • Book 2
    • Chapter 1
    • Chapter 2

So, I add a regular page called My books (slug: my-books) to have a “start page” which describes my collection of books. The url is http://www.example.com/my-books/. On that page I list all my added books and their chapters.

Works perfectly if I click on Book 1 (or Book 2). The url is http://www.example.com/books/book-1/. But if I want to view a chapter (url: http://www.example.com/books/book-1/chapter-1) I get 404’d.

My permalink structure is: /%category%/%postname%/

I really can’t figure out why the custom child pages (posts) 404’s. Since my regular start page “My Books” slug doesn’t clash with either the CPT name or the rewrite slug I guess I’m all out of thoughts atm.

Note: I’ve also tried to remove 'rewrite' => array( 'slug' => 'books') but I still get 404 on the “child posts”.

How do you guys do when creating hierarchical CPTs? Can I make this work or do I need to rethink the structure?

Thank you!

Solutions Collecting From Web of "Child pages on hierarchical Custom Post Types 404s"