2
votes

When setting 'hierarchical' => true to a custom post type and assigning a parent to a page with this custom post type throws 404. It is posible to make this work?

Expected result: http://example.com/parent-page/page-title/

This works with Wordpress normal pages, but not with custom post types (404 not found).

Thanks in advance!

1
Would this permalink structure be OK: example.com/post-type/parent-slug/child-slug? Or would you not want the post-type in the permalink at all?JoeMoe1984
I don't want the post type in the permalink :(Maxi
I just updated my answer now that its clear :)JoeMoe1984

1 Answers

2
votes

Since I just noticed you wanted a permalink structure exactly like pages, you can modify the rewrite parameter when you register your post type. Here is an example on how to make it just like pages structure:

$args = array(
    'labels'              => $labels,
    'hierarchical'        => true, // just to get parent child relationships with your custom post [but you already knew this part]
    'description'         => 'Your custom post type description here',
    'has_archive'         => false,
    'rewrite'             => array('slug' => '/'), // we rewrite the slug to the root url so that it has no post type prefix
    'with_front'          => false, // This is optional but just in case your posts are set to /blog/post-name this will make it /post-name for your custom post type 
    'capability_type'     => 'post',
    'supports'            => array(
        'title', 'editor', 'author', 'thumbnail', 'page-attributes'
        )
);

register_post_type( 'post_type', $args );

Also make sure you update the permalink structure after you make these changes so Wordpress can rewrite the rules.