WordPress Q&As Logo
WordPress Q&As Part of the Q&A Network
Q&A Logo

What’s the best way to register custom post types with hierarchical support?

Asked on Aug 25, 2025

Answer

To register a custom post type with hierarchical support in WordPress, you should use the `register_post_type` function with the `'hierarchical'` argument set to `true`. This allows the custom post type to behave like pages, supporting parent-child relationships.
<!-- BEGIN COPY / PASTE -->
    function my_custom_post_type() {
        $args = array(
            'label'         => 'Books',
            'public'        => true,
            'hierarchical'  => true,
            'supports'      => array('title', 'editor', 'thumbnail'),
            'has_archive'   => true,
            'rewrite'       => array('slug' => 'books'),
        );
        register_post_type('book', $args);
    }
    add_action('init', 'my_custom_post_type');
    <!-- END COPY / PASTE -->
Additional Comment:
  • Setting `'hierarchical' => true` allows the custom post type to have a parent-child structure similar to pages.
  • Ensure the `'supports'` array includes features you want, such as 'title', 'editor', and 'thumbnail'.
  • Use `'rewrite' => array('slug' => 'books')` to customize the URL structure.
  • Always hook your custom post type registration to the 'init' action.
  • Remember to flush rewrite rules after registering a new post type by visiting the Permalinks settings page.
✅ Answered with WordPress best practices.

← Back to All Questions
The Q&A Network