Parent drop-down missing for hierarchical custom post type or Gutenberg

When we are creating ‘hierarchical’ custom post types, sometimes Page Attributes or Parent dropdown is missing. This is due to the required parameter missing in WordPress register_custom_post() function.


Common Issues:

  • ‘Page Attributes’ section is not visible in hierarchical custom post type (similar to page) editor
  • Parent drop-down missing in Gutenberg editor
  • Custom post type does not behaves like pages (parent/child relationship)

Fix/Solution:

In register_custom_post() function, make sure in added ‘page-attributes’ in ‘supports’ and ‘hierarchical’ is set to true.

'supports'     => array('title', 'editor', 'author', 'thumbnail', 'page-attributes'),
'hierarchical' => true,

Additionally, in $labels arguments, if you have empty ‘parent_item_colon’, it cause an issue in Gutenberg and makes parent dropdown hidden. Therefore, either do not use this argument at all or mention some label string.


Complete Code to create hierarchical custom post type:

function cpt_news_function() {
	$labels = array(
		'name'               => _x( 'News', 'post type general name', 'textdomain'),
		'singular_name'      => _x( 'News', 'post type singular name', 'textdomain'),
		'add_new'            => _x( 'Add New', 'textdomain'),
		'add_new_item'       => __( 'Add New News', 'textdomain'),
		'edit_item'          => __( 'Edit News', 'textdomain'),
		'new_item'           => __( 'New News', 'textdomain'),
		'all_items'          => __( 'All News', 'textdomain'),
		'view_item'          => __( 'View News', 'textdomain'),
		'search_items'       => __( 'Search News', 'textdomain'),
		'not_found'          => __( 'No News found', 'textdomain'),
		'not_found_in_trash' => __( 'No News found in the Trash','textdomain'),
		'parent_item_colon'  => 'Parent',
		'menu_name'          => 'News'
	);
	$args = array(
		'labels'        => $labels,
		'description'   => 'Holds our News and News specific data',
		'public'        => true,
		'menu_position' => 5,
		'publicly_queryable' => true,
		'query_var' => true,
		'rewrite' => array('slug' => 'news'),
		'supports'      => array('title', 'editor', 'author', 'thumbnail', 'page-attributes'),
		'hierarchical' => true,
		'has_archive'   => false,
		'show_in_rest'       => true,
		'menu order' => true
	);
	register_post_type( 'my_news', $args );
}
add_action( 'init', 'cpt_news_function' );

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.