0

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:

  1. Group the 3 contents under one menu page called “My Custom Page”
  2. 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”).

0

I found the answer. To make it short:

SOLUTION 1

If you want to simply add a custom post type to a menu item, go with solution number one (which involves creating a menu page with add_menu_pageand setting 'show_in_menu=>' to your menu page slug). It works, but if you click on your newly created menu page, you will get redirected to the first CPT (any subpage will get pushed at the end of the list).

SOLUTION 2

If you want to group you custom post types, click on your menu page and end up on a subpage, then go with solution number 2 (see update above): set 'show_in_menu=> false', then create a function like this:

function create_menupages_252428() {

// https://developer.wordpress.org/reference/functions/add_menu_page/

add_menu_page(
    'Page', // Page title
    'Page', // Menu title
    'manage_options', // Capability
    'page', // Slug
    'mycustompage', // Function name
    'dashicons-format-aside', // Slug
    1 // Order
);

// https://developer.wordpress.org/reference/functions/add_submenu_page/

add_submenu_page(
    'page', // Parent slug
    'subpage', // Page title
    'subpage', // Menu title
    'manage_options', // Capability
    'edit.php?post_type=CPT',  // Slug
    false // Function
);
}
add_action('admin_menu', 'create_menupages_252428');

Once done, if you want to show the menu page as active while operating on your custom post type,

function menu_active_252428() {
global $parent_file, $post_type;
if ( $post_type == 'CPT' ) {
    $parent_file = 'page';
}
}
add_action( 'admin_head', 'menu_active_252428' );

If by any chance you find a better way, feel free to add/correct my solution!

0

Update: Now you just need to create the menu page with:

function create_home_menu(){
    add_menu_page(
        'Página de Inicio',
        'Inicio/Home',
        'manage_options',
        'my_home_menu',
        'mycustompage',
        'dashicons-admin-home',
        2
    );
}
add_action('admin_menu', 'create_home_menu');

And then be sure that when you create a post type add 'show_ui'=>true and 'show_in_menu'=>'my_home_menu' where 'my_home_menu is the slug you gave to the menu created before.

Show UI makes it visible to edit and change, and show in menu adds it to the menu as submenu by default. Now you don't need to add extra function to make the parent menu appear as selected when editing the post type.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.