How to add a custom post type into the sub-menu of a custom menu

Yeah I know the title is a bit weird, but it describes the problem that I faced today. I started developing my first real plugin and I wanted to achieve the following:

  1. create an admin menu page, with function add_menu_page()  -> easy,
  2. add a sub-menu page called “settings”, with function add_submenu_page() -> easy,
  3. register a custom post type and put it under the menu from point 1. -> easy, but not for a beginner  🙂

Maybe a picture will help.

Main admin menu

“TTLM” is a menu page that I created, “settings” is the sub-menu page. I wanted to add a custom post type named “Teams” to be placed in between.

This is how I accomplished it:

 

So basically:

  1. when registering a custom post type set the ‘show_in_menu’ argument to false,
  2. when adding a sub-menu item set the callback function to NULL and the menu_slug to the custom post type, e.g. ‘edit.php?post_type=your_custom_post_type_name_goes_here’

I hope this helps and if you have any questions don’t hesitate to ask in the comments bellow.

 

13 comments on “How to add a custom post type into the sub-menu of a custom menu

  1. Can you please let me know, how to get that submenu( custom post type ) item to highlight when its active.

    Thanks.

  2. Great post. You can also define the location for the post type in the show_in_menu like this:

    ‘show_in_menu’ => ‘edit.php?post_type=ttlm_team’,

  3. when happens when i register taxonomy to that sub-menu CPT? it isn’t appearing. only thing i found is

    add_action(‘admin_menu’, ‘my_admin_menu’);
    function my_admin_menu() {
    add_submenu_page(‘edit.php?post_type=parent_post’, ‘Title, ‘Title’, ‘manage_options’, ‘edit-tags.php?taxonomy=calendar_cat&post_type=parent_post’);
    }

    Title will be appear along with the CPT and not as further sub of child cpt. Is it even possible of not?

Leave a Reply

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