Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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,

share|improve this question

2 Answers 2

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.

share|improve this answer
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).

share|improve this answer
    
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 '11 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 '11 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 '11 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 '11 at 12:53
    
this does not seem to address the question... –  somatic Dec 28 '12 at 6:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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