Search code examples
phpwordpresscustom-post-typepermalinkshierarchical

WordPress CPT hierarchical set to true it turns to 404 page


I have Custmom Post Type and have a rewrite slug, it is working perfectly until I set hierarchical to true.

Hierarchical is working perfectly in wp-admin but when I navigated in front-end the CPT added item goes to 404 page.

Please note that flush_rewrite_rules() is integrated and also I tried many times to resubmit the permalink in settings, using wordpress 4.3.1.

$arg = [
             'public' => true,
             'show_ui' => true,
             '_builtin' => false,
             '_edit_link' => 'post.php?post=%d',
             'capability_type' => 'post',
             'hierarchical' => false,
             'rewrite' => ['slug' => 'book', 'page-attributes'],
             'query_var' => 'book',
             'supports' => ['title', 'editor', 'thumbnail'],
             'menu_position' => 5,
             'menu_icon'=> 'dashicons-testimonial'
        ];

and when I set 'hierarchical' => true, it goes in 404 page.


Solution

  • It looks like you put page-attributes in the wrong array. It belongs with supports, not rewrite! In the array below, I moved it to its proper place. Hopefully this helps.

    $arg = [
             'public' => true,
             'show_ui' => true,
             '_builtin' => false,
             '_edit_link' => 'post.php?post=%d',
             'capability_type' => 'post',
             'hierarchical' => true,
             'rewrite' => ['slug' => 'book'],
             'query_var' => 'book',
             'supports' => ['title', 'editor', 'thumbnail', 'page-attributes'],
             'menu_position' => 5,
             'menu_icon'=> 'dashicons-testimonial'
        ];