If you are working on a plugin and need more than one custom post type, but you don’t want to have a separate admin menu for each custom post type, you can use the workaround below.
This should be useful if you have one main CPT with taxonomy and several other supporting CPTs which may or may not have their own nav menus.
Add Sub Menu
First, we need to add the secondary CPT as a sub-menu link to our main one.
add_submenu_page(
'edit.php?post_type=primary',
'Page Title',
'Menu Title',
'manage_options',
'edit.php?post_type=secondary',
''
);
The snippet above will add the required connection to our secondary post-type main page.
Hide the Menu Tab
If you need your secondary CPT to have show_in_menu
and show_in_nav_menus
If true, then you need to hide the menu with CSS.
Also, I usually use a menu_postion
for secondary CPTs -1
#menu-posts-secondary_cpt {
display: none !important;
}
The above will hide the secondary CPT menu for the navbar.
Keep Main CPT Menu Open
Lastly, you will notice that once you navigate outside the scope of your primary CPT, the menu will close. Since this is not a big issue, you may need to open it for better UX.
To do this, we will use again CSS and target the body
of our page.
You will have the secondary CPT unique class name for all its admin pages in the body classes.
body .post-type-secondary_post_type #menu-posts-primary_cpt .wp-submenu {
position: static;
border-left: 0;
}
You will need to add/remove a couple of classes with jQuery.
$('body.post-type-secondary_cpt li#menu-posts-primary_cpt, body.post-type-secondary_cpt li#menu-posts-primary_cpt > a').addClass('wp-has-current-submenu wp-menu-open').removeClass('wp-not-current-submenu');
Hopefully, this was a helpful and quick workaround when you have multiple CPTs but want to keep them in a single admin menu.
Note: I am using primary_cpt
and secondary_cpt
as placeholders for your CPTs names.
‘Til the next time.