Custom post type hierarchical permalinks not behaving as expected for parent/child pages

When you have Pages with children, the permalink structure works something like this:

example.com/parent-page/child-page/

Read More

All well and good. Trying to go to this URL:

example.com/child-page/

doesn’t work, which is good, and as expected – I only want the one URL for my child page.

However, I’ve set up a custom post type, and set it to be hierarchical, so as to behave like pages. It doesn’t quite work the same way:

example.com/custom-post-type/parent-page/child-page/

works as expected, but removing the parent page from the URL:

example.com/custom-post-type/child-page/

also displays the child page. It should 404, I would have thought.

I’ve used the following args to register_post_type:

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => array('slug'=>'custom-post-type','with_front'=>false),
    'capability_type' => 'post',
    'has_archive' => false, 
    'hierarchical' => true,
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail','excerpt','revisions','page-attributes')
); 

It’s entirely possible I’ve misunderstood some of the options though. Can someone tell me if I’m doing something wrong, or if this is a bug or a feature?

I’m using 3.2.1, with no plugins, just my custom theme.

Thanks,

Related posts

Leave a Reply

2 comments

  1. example.com/custom-post-type/parent-page-1/child-page/
    example.com/custom-post-type/parent-page-2/child-page/
    

    The above could not happen. If you were to give 2 pages the same name (regardless of their parents being different) then WP would amend the slug of one of them.

    example.com/custom-post-type/parent-page-1/child-page/
    example.com/custom-post-type/parent-page-2/child-page-1/
    

    Entering example.com/custom-post-type/child-page/ or example.com/custom-post-type/child-page-1/ would work as expected, but I believe that example.com/custom-post-type/parent-page-2/child-page/ would redirect you to example.com/custom-post-type/parent-page-1/child-page/ (although I am not 100% on that).