Add a Custom Post Type Submenu To Another Post Type Menu

Custom Post Types are the most powerful features of WordPress, specially if you’re in the business of creating custom solutions for clients beyond normal bloging functionality.

Introducing a new menu in the WordPress dashboard is really easy when using the custom post type API; however, what about the case when you have a custom post type and is a child of another post type?

Specifically, what about the case when you want to add a custom post type submenu to an existing post type menu?

First, let’s introduce a Testimonial custom post type. This will be a top-level menu in the WordPress dashboard.

 /**
        * Define the 'Testimonial' post type.
        *
        * This will be our top-level custom post type menu.
        */
        $args = array(
        'labels'    =>    array(
        'all_items'    =>    'All Testimonial',
        'menu_name'    =>	'Testimonial',
        'singular_name'       =>	'Testimonial',
        'edit_item'           =>	'Edit Testimonial',
        'new_item'            =>	'New Testimonial',
        'view_item'           =>	'View Testimonial',
        'items_archive'       =>	'Testimonial Archive',
        'search_items'        =>	'Search Testimonial',
        'not_found'	          =>	'No testimonial found',
        'not_found_in_trash'  =>	'No testimonial found in trash'
        ),
        'supports'		=>	array( 'title', 'editor', 'author', 'revisions' ),
        'menu_position'	=>	5,
        'public'		=>	true
        );
        register_post_type( 'testimonial', $args );
    

Now, we can introduce a second post type, say, Client that will be used to represent all of the locations that we’ve used throughout the testimonial.

        /**
        * Next, we'll define a second custom post type called 'Client' where we could
        * potentially display a list of client that are used as part of our testimonial.
        *
        * This custom post type will be added as a submenu to the 'Client' menu<br>
        */
        $args = array(
        'labels'    =>	array(
        'all_items'           =>    'Client',
        'menu_name'	          =>    'Client',
        'singular_name'       =>    'Client',
        'edit_item'           =>    'Edit Client',
        'new_item'            =>    'New Client',
        'view_item'           =>    'View Client',
        'items_archive'       =>    'Client Archive',
        'search_items'        =>    'Search Client',
        'not_found'	          =>    'No client found.',
        'not_found_in_trash'  =>    'No client found in trash.'
        ),
        'supports'        =>    array( 'title', 'editor', 'revisions' ),
        'show_in_menu'    =>    'edit.php?post_type=testimonial',
        'public'		  =>    true
        );
        register_post_type( 'testimonial', $args );
    

Note that the following line is key in adding the menu as a submenu:

        'show_in_menu'    =>    'edit.php?post_type=testimonial', 
        /* 
        * testimonial is post type. you can change it as you want.
        */
    

Leave a Reply

Your email address will not be published. Required fields are marked *