Plugin: Custom Post Type Auto Menu

NOTE: The plugin has been updated since this post to handle multiple custom post types. This article still gives the basics but the settings have changed since then. Read about it here.

While working on a recent site, I realised that a simplified method for adding a new custom post type post to a menu was needed for the sake of the clients, who are not used to working with a CMS. In this case, the ‘Projects’ custom post type is used to encapsulate all the photos and information they would like to display related to each of their construction projects. The projects are listed on the ‘Projects’ page and displayed in the corresponding menu.

With the standard method, it would be neccessary to add the new project custom post type, then go to ‘Appearance > Menus’, add a checkmark to the new project, select ‘Add to Menu’, drag the newly added item to the ‘Projects’ parent menu item so that it becomes a sub-menu item, and then save the menu. With such a multi-step and laborious process the chance for errors — or having to explain the method over and over — was very high.

Standard cpt menu method

Considering that WordPress already allows for automatically populating menus for categories and pages, and that even with custom menus top-level pages can be be added automatically, I figured it would be an easy matter to make that possible with custom post types. Not so. There is a lot missing in the codex in regards to custom menus.

Eventually I figured it out and got a hard-coded example working. I decided to expand on it and make it a plugin, since it solved my problem so well. It’s now in the WordPress plugin repository and also available on Github.

Using the plugin is simple. Install in the usual way, activate, and ‘CPT Auto Menu’ will appear in the Settings. There are only three parameters to be set:

1. The custom post type
2. The menu
3. The parent menu item.

In the case of my example, the custom post type is ‘Projects’, the menu is ‘Main Nav’ and the parent menu item is the page ‘Projects’. Now, whenever a new Project is created, it is automatically added to the ‘Projects’ menu item in the ‘Main Nav’ menu. That’s it!

CPT Auto Menu settings

CPT Auto Menu menu added

Similarly, when a Project is deleted, it is removed from the menu. One caveat: a deleted Project is NOT moved to the Trash, it is deleted entirely. This is due to the nature of the custom menu and the verifications we need to do to ensure that a new menu item is not added twice.

This simple utility plugin saves the end-user from having to do an additional 5 steps to add a custom post type item to the menu. It solved my problem and made the process much easier for my client. Hopefully others will find it equally useful.


    • badfun

      Thank you Tayyab. At the moment it can only do one, but I will work on implementing multiple instances. It is class based, so should be a matter of adding a new instance of the class for each custom post type.

Add Comment Register

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>