Parent child relationship in custom post type

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.

1
1

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.

Leave a Comment