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.

  1. Chris

    Hey I’m wondering about two things:

    1. I already created a couple of post items before I setup the menu. They aren’t appearing because I guess it will only discover the posts after the menu has been setup. But is there a way for it to discover these items by possibly editing the post items? Right now it doesn’t see all the already created custom post type items.

    2. Is there a way to order the items by alphabetical or some other arrangement???

    • Ken Dirschl

      Hi Chris,
      Adding existing cpt’s is on my To Do list, but not implemented. Sorry. But they should be appearing in your custom menu page where you can just add them to the menu. If you don’t see them, check your ‘Screen options’ tab and your custom post types should be listed there.

      Ordering the menu is done by drag and drop (or ID number when you first add items). I didn’t try to extend that anyway with the plugin.
      Let me know if you have any more questions.

  2. Chris

    Thanks Ken. So I got my existing custom post types to appear by changing their state first to Draft, then back to Published.

    The ordering is easy with drag and drop – but it would be mighty cool at some point to include some type of ordering options – like ordering by second word, first word, or published date, or modified date. etc. There’s probably some sort of function I could include in the theme’s functions file, right? That ability is currently beyond me. In any case, thanks for the cool plugin!!!

    • Ken Dirschl

      hi Chris,
      thanks for your comment. sorry for the tardy response but many server problems to deal with.
      I’m making a list of future improvements and ordering the menu is on it. This gets into core WP functionality though so it could create more problems than it solves. On the other hand, maybe it’ll make it to core! 🙂
      thanks for your input.

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=""> <s> <strike> <strong>