1

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”).

3 Answers 3

2

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.

0

@cerere , To be active in the submenu only need 'show_in_menu' => false in the argument.

no need to add this function.

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

Menu

add_menu_page(
        'Main menu', // The title to be displayed in the browser window for this page.
        'Main menu', // The text to be displayed for this menu item
        'manage_options', // Which type of users can see this menu item
        'plugin_demo_options', // The unique ID - that is, the slug - for this menu item
        array($this, 'call_back_function') // The name of the function to call when rendering this menu's page
    );

    add_submenu_page('plugin_demo_options', 'Submenu Page Title', 'Whatever You Want', 'manage_options', 'my-menu', 'acutions_customers_spendings_list');
    add_submenu_page('plugin_demo_options', 'Test Settings', 'Test Settings', 'manage_options', 'edit.php?post_type=books', false);

Custom Post type

 /*Custom Post type start*/
function post_type_book()
{
    $supports = array(
        'title', // post title
        'author', // post author
        'thumbnail', // featured images
        'excerpt', // post excerpt
        'custom-fields', // custom fields
        'comments', // post comments
        'revisions', // post revisions
        'post-formats', // post formats
    );
    $labels = array(
        'name' => _x('Books', 'plural'),
        'singular_name' => _x('Book', 'singular'),
        'menu_name' => _x('Books', 'admin menu'),
        'name_admin_bar' => _x('Books', 'admin bar'),
        'add_new' => _x('Add Book', 'add new'),
        'add_new_item' => __('Add New Book'),
        'new_item' => __('New Book'),
        'edit_item' => __('Edit Book'),
        'view_item' => __('View Book'),
        'all_items' => __('All Book'),
        'search_items' => __('Search Book'),
        'not_found' => __('No Bookfound.'),
    );
    $args = array(
        'taxonomies'  => array(''),
        'supports' => $supports,
        'labels' => $labels,
        'public' => true,
        'show_in_menu' => false,
        'show_in_nav_menus'   => false,
        'show_in_admin_bar'   => false,
        'query_var' => true,
        'rewrite' => array('slug' => 'books', 'with_front' => false, 'pages' => true, 'feeds' => true,),
        'has_archive' => false,
        'hierarchical' => false,
    );
    register_taxonomy(
        'book-category',
        array('books'),
        array(
            'hierarchical' => true,
            'label' => 'Categories',
            'singular_label' => 'Category',
            'rewrite' => array('slug' => 'book-category')
        )
    );
    register_post_type('books', $args);
}

Screenshot

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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