CPT Parent as a page and CPT Child as a posts

I have one CPT page named as books and one CPT post named as chapters, I am trying to achive this permalink structure here

book1->chapter11
book1->chapter12

book2->chapter21
book2->chapter22

and so on.

Read More

Here is my code for books cpt

register_post_type( 
        'books',
        $labels
        ,
            'public'                =>  true,
            'exclude_from_search'   =>  false,
            'publicly_queryable'    =>  true,
            'show_ui'               =>  true,
            'show_in_nav_menus'     =>  true,
            'show_in_menu'          =>  true,
            'show_in_admin_bar'     =>  true,
            'menu_position'         =>  100,
            'capability_type'       =>  'page',
            'map_meta_cap'          =>  true,
            'hierarchical'          =>  true,
            'supports'              =>  array(
                    'title',
                    'editor',
                    'author',
                    'thumbnail',
                    'comments',
                    'page-attributes',          
            ),
            'has_archive'           =>  true,
            'rewrite'               =>  array(
                    'slug'          =>  'book',
                    'with_front'    =>  true,
                    'feeds'         =>  true,
                    'pages'         =>  true
            ),
            'query_var'             =>  true,
            'can_export'            =>  true, 
        ) 

    );

and for chapters cpt

register_post_type( 
        'chapters',
$labels
,
            'public'                =>  true,
            'exclude_from_search'   =>  false,
            'publicly_queryable'    =>  true,
            'show_ui'               =>  true,
            'show_in_nav_menus'     =>  true,
            'show_in_menu'          =>  'edit.php?post_type=book',
            'show_in_admin_bar'     =>  true,
            'menu_position'         =>  100,
            'capability_type'       =>  'post',
            'map_meta_cap'          =>  true,
            'hierarchical'          =>  true,
            'supports'              =>  array(
                    'title',
                    'editor',
                    'author',
                    'thumbnail',
                    'comments',
                    'page-attributes',
                    'post-formats'                    
            ),
            'has_archive'           =>  true,
            'rewrite'               =>  array(
                    'slug'          =>  'book/chapter',
                    'with_front'    =>  true,
                    'feeds'         =>  true,
                    'pages'         =>  true
            ),
            'query_var'             =>  true,
            'can_export'            =>  true,           
        ) 

    );

these url’s are working fine

localhost/library/book/html
localhost/library/chapter/preface

individually, but when I try this url

localhost/library/book/chapter/preface

currently as per my cpt settings, its gives me 404 error, I tried template redirect and rewrite flush also, but nothing works, now my question are

  1. is it possible Parent as a page and Children as a posts?
  2. if yes what’s wrong with this code, am I am missing here?
  3. my main concern is here url rewriting, it should be

    localhost/library/html/preface

where html is the name of the book and preface is the chapter.

I searched lot here but didn’t get the solution it might look similar question to you, but really I need help.

thanks.

Did some changes

like changed parent slug to books and child slug to book/chapters and query_var to chapter so working fine for my question 1 and 2 now looking for 3 question url_rewriting.

thanks.

Related posts

Leave a Reply