This website uses cookies to allow us to see how the site is used. If you continue to use this site, we assume that you are okay with this. If you want to use the sites without cookies, please see our privacy policy.

Add a Custom Post Type Submenu To An Existing Custom Post Type Menu

Custom Post Types are on of the most powerful features of WordPress, especially if you’re in the business of creating custom solutions for clients beyond normal blogging functionality.

Introducing a new menu in the WordPress dashboard is really easy when using the custom post type API; however, what about the case when you have a custom post type and is a child of another custom post type?

Specifically, what about the case when you want to add a custom post type submenu to an existing custom post type menu?

First, let’s introduce a Portfolio custom post type. This will be a top-level menu in the WordPress dashboard.

/**
* Define the 'Portfolio' post type. This is used to represent galleries
* of photos.
*
* This will be our top-level custom post type menu.
*/
$args = array(
'labels' => array(
'all_items' => 'Gallery',
'menu_name' => 'Portfolio',
'singular_name' => 'Gallery',
'edit_item' => 'Edit Gallery',
'new_item' => 'New Gallery',
'view_item' => 'View Gallery',
'items_archive' => 'Gallery Archive',
'search_items' => 'Search Portfolio',
'not_found' => 'No galleries found',
'not_found_in_trash' => 'No galleries found in trash'
),
'supports' => array( 'title', 'editor', 'author', 'revisions' ),
'menu_position' => 5,
'public' => true
);
register_post_type( 'portfolio', $args );

Now, we can introduce a second post type, say, Locations that will be used to represent all of the locations that we’ve used throughout the portfolio.


/**
* Next, we'll define a second custom post type called 'Locations' where we could
* potentially display a list of locations that are used as part of our portfolio.
*
* This custom post type will be added as a submenu to the 'Portfolio' menu
*/
$args = array(
'labels' => array(
'all_items' => 'Locations',
'menu_name' => 'Locations',
'singular_name' => 'Location',
'edit_item' => 'Edit Location',
'new_item' => 'New Location',
'view_item' => 'View Location',
'items_archive' => 'Location Archive',
'search_items' => 'Search Locations',
'not_found' => 'No locations found.',
'not_found_in_trash' => 'No locations found in trash.'
),
'supports' => array( 'title', 'editor', 'revisions' ),
'show_in_menu' => 'edit.php?post_type=portfolio',
'public' => true
);
register_post_type( 'location', $args );

Note that the following line is key in adding the menu as a submenu:


'show_in_menu' => 'edit.php?post_type=portfolio',

And that’s it! Easy enough, right?

About

I'm a Senior Backend Engineer at WebDevStudios and I can often be found of software engineering within the context of blogging on my own site.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.