2

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

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

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,

2 Answers 2

1

I have solved the answer to this problem by doing this:

echo get_permalink( $page->ID );

This gave me the right links to the pages.

0
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).

5
  • That's not the way it's working for me. I'm using 3.2.1 (should have mentioned that, will edit my main post to put it in), and can have example.com/custom/pagename, example.com/custom/parent1/pagename and example.com/custom/parent2/pagename - Wordpress happily creates them all automatically, without me editing the slug.
    – Dan
    Nov 9, 2011 at 11:45
  • From the WP Codex for Pages → Add New - Avoid using the same title twice as that will cause problems. Maybe when adding/editing pages WP doesn't check for a unique slug, but it really should, else you will get all sorts of issues. The title can be the same, but the slug MUST be different - it is as unique as the ID.
    – David Gard
    Nov 9, 2011 at 12:07
  • I've amended my question, since this point wasn't what I was actually asking, just a way of illustrating the main problem. The duplicate name is easily fixed with a little reorganization, but I'm mostly interested in stopping example.com/custom/parent/child being also available at example.com/custom/child.
    – Dan
    Nov 9, 2011 at 12:33
  • Id suggest having a look at the rewrite rules - $wp_rewrite is the global for that. It is possible to edit them, and probably this is just the default, if undesired, action.
    – David Gard
    Nov 9, 2011 at 12:53
  • this does not seem to address the question...
    – somatic
    Dec 28, 2012 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.