Home » Wordpress » Missing taxonomy menu when custom post type has show_in_menu set

Missing taxonomy menu when custom post type has show_in_menu set

Posted by: admin November 30, 2017 Leave a comment

Questions:

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:

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?

Answers:

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];
});

Questions:
Answers:

It’s quite simple. Add 'menu_name' => 'Docs',

Check WordPress Codex for details: http://bitly.com/194qjN3

Questions:
Answers:

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');
}