So, in my custom Wordpress plugin I have two custom post types. My customer wants to have them grouped under one main admin navigation item.

I go and create the main admin item using:


As expected it appears below the dashboard, so far everything's great. Now I create my two post types (I'll only show it for one below, since they are not that different) and tell them to get in below this main navigation item:

$args = [
    ...some settings, 
    'show_in_menu' => 'my-plugin-name-main'
register_post_type( 'myfirstposttype', $args );

And yes, they both appear below my main navigation point, but where the post types before created four navigation elements (name of the post type, create, categories, tags) it now creates only one (the name of the post type).

Weird flex, but okay ...

So I add "create" navigation item for each post type by hand:

    'Create Post Type 1',
    'Create Post Type 1',

But now the order is not so nice, since I get post type 1, post type 2, then create post type 1 and create post type 2. Plus, we want the handling of tags and categories also below our main nav point, so we have to go the extra mile and reorder the whole submenu:

add_filter('custome_menu_order', '__return_true');
add_filter('menu_order', [$myPlugin, 'reorder_menu_entries']);

public function reorder_menu_entries($menu_order) {
    global $submenu;
    $newArray = [];
    $newArray[] = $submenu['my-plugin-name-main'][0]; // Post type 1
    $newArray[] = $submenu['my-plugin-name-main'][2]; // Create Post type 1
    $newArray[] = $submenu['my-plugin-name-main'][1]; // Post Type 2
    $newArray[] = $submenu['my-plugin-name-main'][3]; // Create Post type 2
    $newArray[] = $submenu['edit.php'][15]; // Handling of categories
    $newArray[] = $submenu['edit.php'][16]; // Handling of tags

    // Remove it from the original menu item

    $submenu['my-plugin-name-main'] = $newArray;
    return $menu_order;

And while this does in fact give me the menu as I want it, when I now click on categories or tags Wordpress will still open the main menu point of "posts" instead my new custom one. Which is weird, since I expected it to check the global $submenu variable to check where a navigation element is located?!

Is there a way to seamlessly handle this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.