Missing taxonomy menu when custom post type has show_in_menu set

I crete a custom post type with a taxonomy and all works fine.
WordPress create a main menu:

MyCustomPost
   +----------> MyCustomPost
   +----------> Add New
   +----------> My Taxonomy

However, I like to create a my own menu, like this:

Read More
MyTitle
   +----------> MyCustomPost
   +----------> Add New
   +----------> My Taxonomy

So, I create a my own menu:

        $menu_main = add_menu_page( 'MyMenu', 'MyMenu', 'edit_posts', 'my_menu_key', array( $this, 'menu_main'), $icon_menu, 100 );

When register my custom post type I set show_in_menu with my_menu_key, and its works.

MyMenu
   +----------> MyCustomPost

The question is: where is taxonomy? and `Add New sub menu?
Any idea?

Related posts

Leave a Reply

2 comments

  1. Nice trick with show_in_menu. But the taxonomy sub-menu is built in a hard-coded manner and register_taxonomy doesn’t take any parameter like that. Probably, the Add New submenu works the same way.

    A workaround is to manipulate the global $submenu:

    // PHP 5.3+ anonymous function
    add_action( 'admin_init', function() {
        global $submenu; 
        $submenu['my_menu_key'][10] = $submenu['edit.php?post_type=YOUR-CPT-SLUG'][10];
        $submenu['my_menu_key'][15] = $submenu['edit.php?post_type=YOUR-CPT-SLUG'][15];
    });
    
  2. Instead of manipulating the $submenu global, you can accomplish this by using built-in WordPress functions for better future compatibility.

    add_action('admin_menu', 'add_tax_menus');
    function add_tax_menus() {
        $key = 'edit.php?post_type=CUSTOM_POST_TYPE_SLUG';
        add_submenu_page($key, 'My Page Title', 'My Menu Title', 'manage_categories', 'edit-tags.php?taxonomy=TAXONOMY_SLUG&post_type=CUSTOM_POST_TYPE_SLUG');
    }