Adding Post Types and Taxonomies to Custom Admin Menu Pages

Customizing the WordPress admin menu is something you’ll eventually need to or want to do at some point. Customizing admin menus with Post Types is pretty easy compared to Taxonomies.

When you create Custom Post Types and Custom Taxonomies in WordPress using register_post_type(); and register_taxonomy(); there’s a parameter for each function called show_in_menu.

If you look at register_post_type(); the parameter accepts a simple true or false but also accepts a string as an argument for show_in_menu:

show_in_menu
(boolean or string) (optional) Where to show the post type in the admin menu. show_ui must be true.

Default: value of show_ui argument

‘false’ – do not display in the admin menu
‘true’ – display as a top level menu
‘some string’ – If an existing top level page such as 'tools.php' or 'edit.php?post_type=page', the post type will be placed as a sub menu of that.

Note: When using ‘some string’ to show as a submenu of a menu page created by a plugin, this item will become the first submenu item, and replace the location of the top-level link. If this isn’t desired, the plugin that creates the menu page needs to set the add_action priority for admin_menu to 9 or lower.

Note: As this one inherits its value from show_ui, which inherits its value from public, it seems to be the most reliable property to determine, if a post type is meant to be publicly useable. At least this works for _builtin post types and only gives back post and page.

If you look at register_taxonomy(); the parameter accepts a simple true or false but DOESN’T accept a string as an argument for show_in_menu like register_post_type(); does:

show_in_menu
(boolean) (optional) Where to show the taxonomy in the admin menu. show_ui must be true.
Default: value of show_ui argument

‘false’ – do not display in the admin menu
‘true’ – show as a submenu of associated object types

Adding a Custom Post Type to a custom admin menu is simple. Just specify the slug of the custom admin menu as an argument for the show_in_menu parameter while using register_post_type();.

How about adding a Custom Taxonomy to a custom admin menu? That’s a bit tricky to do.

What I’m about to show you works for both Custom Post Types AND Custom Taxonomies.

Step 1 – Register Custom Post Types and Custom Taxonomies

Hook into init and register Custom Post Types and Custom Taxonomies.

 

Step 2 – Create Parent Admin Menu Item and Admin Submenu Items

Hook into admin_menu to create a custom parent admin menu, and add Custom Submenu Admin Pages, Custom Post Type pages, and Custom Taxonomy Pages all to the custom parent admin menu.

 

Step 3 – Highlight Appropriate Admin Submenu Item

Hook into parent_file to correctly highlight your Custom Post Type and Custom Taxonomy submenu items with your custom parent menu/page.

If you need any clarification about how any of this works, read the following pages from top to bottom:

  1. Adding Custom Parent Admin Menus
  2. Adding Custom Child Admin Menus
  3. Roles and Capabilities in WordPress
  4. Registering Custom Post Types
  5. Registering Custom Taxonomies
  6. WordPress Plugin API :: Action Reference
  7. WordPress Plugin API :: Action Reference :: init
  8. WordPress Plugin API :: Action Reference :: admin_menu
  9. WordPress Plugin API :: Filter Reference
  10. List of All WordPress Hooks (including actions and filters)

Credits

Original question asked by @numediaweb on WordPress Stack Exchange: Show custom taxonomy inside custom menu.
Answer to Question: Show custom taxonomy inside custom menu by @Michael Ecklund on WordPress Stack Exchange.

Related Questions

ThisĀ article assists in answering all of the following related questions:

  1. [WordPress Development] Show custom taxonomy inside custom menu asked by: @numediaweb
  2. [WordPress Development] How do I create multiple post types in same menu section in WP-admin? asked by: @Peter Westerlund
  3. [WordPress Development] Show custom taxonomies in admin panel under custom post type asked by: @Eoghan OLoughlin
  4. [WordPress Development] How to add a taxonomy into admin menu asked by: @genuy11512

12 Replies to “Adding Post Types and Taxonomies to Custom Admin Menu Pages”

  1. I’m unable to get your code working in the newest version of WordPress. I’ve even tried copying it exactly from your page into a new plugin called news/news.php and activating it and nothing. It doesn’t show any new menus on the admin menu.

    1. Please double and possibly triple check everything. Make sure there are no typos. Make sure you understand how hooks work. Make sure you understand the plugin API. Let me know if everything seems on-point and still isn’t working.

  2. Thanks for this tutorial! I have 14 custom post types and each requires documentation and has custom settings so this really helped me to get all of the in line and displayed perfectly.

    The issue I’m having is with step three. On menus where I have several taxonomies, I can only get the last taxonomy added to the function to be highlighted.

    I tried altering the if($pagenow==edit_tags.php) line to include the specific taxonomy and post type but then none of the taxonomies are highlighted.

    I also tried adding the additional taxonomies as additional lines in the if statement but tstill only the last taxonomy added is highlighted.

    The documentation on parent_file in the codex is pretty vague.

    Is this a case where I would use an ifelse or a switch statement?

    Any help would be appreciated!

    Thanks!

    1. I was able to solve this but perhaps there is a more elegant solution.

      I simple wrapped each if ( $pagenow == 'edit-tags.php' ) statement in an additional check using: if ( $current_screen->taxonomy == 'MY_TAXONOMY' ).

      This allows multiple taxonomies to be highlighted as they are displayed in the admin section.

      Let me know if you can think of an easier or simpler way to do this.

      Thanks so much!

    2. You’re very welcome! Thank you for the comment(s). Yes, you’re on the right track. You’ll need to specify detailed conditions to match your taxonomy appropriately. You could try using get_query_var( 'taxonomy' ); or if that doesn’t work, you could use $_GET[ 'taxonomy' ]; You could also investigate the WP_Screen object using get_current_screen(); or global $current_screen.

  3. Hi Michael,

    Is there anyway to have only one taxonomy admin panel between custom post types?

    For example, my blog posts and my CPT share the same two taxonomies (location and niches). The list of locations and niches are going to be the same for both blog posts and the CPT. So is there anyway I can have just one place to add locations and niches instead of having multiple taxonomy admin panels?

    Thanks,
    Roselle

    1. That’s essentially what this article is meant to cover.

      To quickly recap:

      1. Step 1: Register Custom Post Types & Custom Taxonomies. During registration, specify show_in_menu => false.
      2. Step 2: Add parent menu add_menu_page(), and/or add submenus add_submenu_page().
      3. Step 3: If a submenu was added to any parent menu, other than the default. The highlighting and menu expansion most likely won’t work. This needs to be corrected using the filter parent_file.

      Everything you need to do is outlined above the comments in my article. If there’s anything which is unclear about my article, please let me know so I can clear that up for you and/or anyone else who may feel the same.

  4. My website has so many taxonomy, can you help me show only one taxonomy selected on wp admin edit post ?

    current I set hidden taxonomy with show_ui is false.

    register_taxonomy('developer', 'post', array(
    'hierarchical' > true,
    'labels' > $labels, /* NOTICE: Here is where the $labels variable is used */
    'show_ui' > FALSE,
    'query_var' > true,
    'rewrite' > array('slug' > 'developer'),
    ));

    1. @thoman

      I would suggest you get familiar with the following areas of WordPress Development:

      Once you’ve familiarized yourself with these areas of WordPress development; I encourage you to come back to this post and read it again.

      This article simply aims at controlling which menus/submenus your custom Taxonomy pages and custom Post Type pages are displayed (Rather than the default behavior of a standalone parent menu for Post Types and/or associated child menu for Taxonomies.). This is particularly useful if you have a plugin which creates it’s own administration menu, and you want your Post Types and/or Taxonomies listed under the same administration menu.

  5. THANK YOU! I have been searching and searching as to why custom taxonomies would not display in my custom admin menus when it is so easy with custom post types. Thanks for your solution!

Leave a Reply