Hierarchical Custom Post Types in WordPress

You need to set both of these things to make it work correctly:

register_post_type( 'docs',
  array(
    'labels'        => array(
      'name'          => __( 'Documentation' ),
      'singular_name' => __( 'Documentation Page' ),
      'add_new'       => __( 'Add Documentation Page' ),
      'add_new_item'  => __( 'Add New Documentation Page' ),
      'edit_item'     => __( 'Edit Documentation Page' ),
    ),
    'public'       => true,
    'has_archive'  => true,
    'rewrite'      => array( 'slug' => 'docs' ),
    'show_in_rest' => true,
    <strong>'hierarchical' => true,
</strong>    'supports'     => array( 'title', 'editor', 'thumbnail', 'excerpt', <strong>'page-attributes'</strong> )
  )
);Code language: PHP (php)

Now when you are on the Create/Edit screen for the new Post Type, you’ll see the Page Attributes area that allows you to select the Parent you’d like.

🤘

CodePen

I work on CodePen! I'd highly suggest you have a PRO account on CodePen, as it buys you private Pens, media uploads, realtime collaboration, and more.

Get CodePen PRO

One response to “Hierarchical Custom Post Types in WordPress”

  1. N.A. Ferrell says:

    I followed a guide to create custom post types with a plugin on my site, but I did not know about the possibility of making the hierarchical. May have to make use of this – thank you for the guide.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top ⬆️