Group Custom Post Types in a Top Level WordPress Menu

Posted By: Proper Noun Staff Posted On: April 9, 2021 Share:

There are many times when I’m working on a custom WordPress website that I find myself having to create several custom post types to improve the clients user experience. In most cases I find that some of these post types would make sense being grouped together under a single menu item. Not only does this help keep the WordPress admin menu from being cluttered with too many items, but it also helps to logically group related content together.

When I first looked up how to create this relationship between post types, I had trouble finding what I was looking for. It turns out that $args argument for the register_post_type function has an option called show_in_menu. This allows us to specify a parent menu for the custom post type to be located under. I found that I usually don’t need a real relationship between the post types, as long as they are grouped together in a way that makes sense to the end user.

Creating a new menu item in the WordPress admin panel

The first thing we need to do is create a new menu item that we will use to group our custom post types under.

For example, if we had post types related to cars we might have a top level menu called cars, and then have custom post types called ‘muscle cars’, ‘exotic imports’, or ‘hybrid cars’ grouped together under this menu item.

We need to use the add_menu_page function to create our top level menu:

add_menu_page takes several parameters: add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); the $menu_title parameter is what the name of our top level menu item will be and $menu_slug is the name that we will provide to our custom post types.

So for our cars example we could do this:


add_action( 'admin_menu', 'register_my_page' ); 
function register_my_page() { 
add_menu_page( 'Cars, 'Cars', 'edit_others_posts', 'cars_menu, function() { echo ‘cars page’ }, dashicons-arrow-right, 6 );
}

How to add a custom post type to our new menu

Now when we create the $args for our custom post types we just need to make sure we have:


‘show_ui’ => true,
‘show_in_menu’ => ‘cars_menu’,

We provide our menu slug name to the show_in_menu parameter and our custom post type will show in the cars menu item in our WordPress admin page.

Hopefully this WordPress pro tip will help you improve your custom templates as much as it has ours.

Proper Noun Staff

Proper Noun Staff

In-House Writing Team

Read informative articles, insights, and other resources right from the experts on the Proper Noun team.

Copyright © Proper Noun 2024