TLDR: While in the backend, I would like to add custom post type inside a menu page (which I can do). But I cannot order the resulting submenu pages.
I have 3 custom post types “A”, “B”, “C”, and I want to:
- Group the 3 contents under one menu page called “My Custom Page”
- When I click “My Custom Page”, being redirected to the content of “My Custom Page”, and not one of the custom post types.
I accomplished the first half quite easilly: I created a menu pages as follows:
add_menu_page('My Custom Page', 'My Custom Page', 'manage_options', 'my-top-level-slug');
And then, I set each custom post type like this:
'show_in_menu'=> 'my-top-level-slug'
This allows me to successfully group my 3 custom post types under one single menu page. And here lies the problem: if I click on “My Custom Page”, I get redirected to the first custom post type (depending on the inclusion order) – I would like to click on “My Custom Page” and get redirected to said page (where I plan on list show stats, most viewed posts, etc), but instead a custom post type opens up, which I don’t really want.
I figured I could add a submenu page, but any submenu page I add gets included AFTER the custom post types (so at the bottom of the submenu pages list). So I’m wondering if is there a way to assign an order to those subpages, so that if I click over “My Custom Page”, it’s not gonna show a custom post type.
UPDATE
After some searching, I found the following article on the Codex: https://developer.wordpress.org/reference/functions/add_submenu_page/ . By following this example and embeding this code:
function wpdocs_register_my_custom_submenu_page() {
add_submenu_page(
'my-menu',
'My Custom Submenu Page',
'My Custom Submenu Page',
'manage_options',
'edit.php?post_type=CPT-NAME',
false
);
}
add_action('admin_menu', 'wpdocs_register_my_custom_submenu_page');
I'm able to accomplish the 2 points above (so the grouping works and when I click I get redirected to a review page). But - by doing so, once I enter inside a post (create or edit one, doesn't matter) the menu page does't result active anymore: if I hover on the menu page, it's marked in white as in active, but otherwise the menu & submenus are collapsed/closed.
The previous solution was working correctly (menu state wise), but didn't allow me to assign a custom page to “My Custom Page”. As for the second solution, it's vice versa (menu state is inactive while inside a post, but I can show “My Custom Page”).