Up until this point, I’ve never worked on a project or done any type of work that required a custom post type to be added to an existing menu in the WordPress dashboard.
For the most part, I’m generally of the mindset that custom post types should:
- Exist as top level menus
- Should be added at the bottom of the WordPress dashboard menu
This mentality is primarily motivated by the fact that I see the core WordPress menu options as first-class citizens in the dashboard, custom post types as being second-class citizens.
That’s just a rule of thumb, though. There are always exceptions.
But there are also times where custom post types could be treated as, say, third-class citizens where they should be integrated with an existing menu be it a core menu or another custom post type menu.
Luckily, it’s trivially easy to add a custom post type as a menu item to an existing menu.
Adding The Custom Post Types
To demonstrate this, we’ll add two custom post types: Portfolios and Locations.
The portfolio is meant to represent a collection of galleries, so this would ideally create a ‘Portfolio’ menu where the user would be able to add a new, edit, search, and manage galleries:
// 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 );
This will introduce the Portfolio menu into the WordPress dashboard menu. If you’ve been working with custom post types for a while, there’s nothing really new in this specific code.
Next, we’ll add the menu for the Locations. It’s roughly the same as the code above except we aren’t including a
Instead, we’re adding a parameter for the
// 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 );
Notice that the
show_in_menu parameter accepts a URL of the page to which this menu should be added.
edit.php?post_type=portfolio is obviously the URL for our Portfolio custom post type; however, you could potentially specify the URL to, say, simply
edit.php to add the menu to the Posts menu.
Get The Gist
Anyway, easy enough, right?
I’ve created a gist of this entire code that can be viewed here on GitHub. Feel free to add comments or modifications as needed as this is primarily for demonstration purposes.
It obviously would need to be registered with the proper hooks and actions to be integrated into WordPress.