9

How can I maintain the parent child relationship in custom post types, so that I have a uniform URL structure? I want to make a URL structure up to 4 levels, e.g.

example.com/sponsor-child/disadvantaged-community/gita-magar

Is it possible without a plugin? When I go to a single page URL, it ends in 3 levels.

11

In your register_post_type call, make sure you have these arguments:

register_post_type(
    'my_post_type',
    array(
        'hierarchical' => true,
        'public' => true,
        'rewrite' => array(
            'slug'       => 'my_post_type',
            'with_front' => false,
        ),
        'supports' => array(
            'page-attributes' /* This will show the post parent field */,
            'title',
            'editor',
            'something-else',
        ),
        // Other arguments
    )
);

Make sure your permalinks are flushed (just visit the Settings > Permalinks page).

Now when you create a new my_post_type, just set it's parent to another and it's permalink will look something like:

http://example.com/parent-post-type/my-post-type/

You can go as many levels as you need.

  • 1
    Can you give me entire code of creating custom post type with parent child relation and URL level to 4 levels for making uniform structure of URL. – user3445146 Apr 16 '15 at 10:34
  • Works fine for me - technically, there shouldn't be any limit. Have you tested this in a fresh install of WordPress with all plugins disabled, and then flushed permalinks? – TheDeadMedic Apr 16 '15 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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