Hide a taxonomy metabox in the post editor


I was recently building a custom vendor profile system for a client, where she could invite vendors that she worked with to create profiles on her site, to show them off as people that she worked with and trusted.

In doing so, I created a few custom taxonomies for her new custom post type and I used CMB 2.0 to provide the interface for adding all of the custom metadata to each post.

There were some custom taxonomies that we only wanted to allow one entry for (such as vendor type), so we created drop-down fields for them. Other taxonomies could have several appropriate terms for each vendor (like geographical areas served).

The issue was that when we registered the custom taxonomies, WordPress added a little metabox in the right column of the post editor for selecting these items. That wouldn’t be so bad if we didn’t want to restrict some taxonomies to just one entry, because we could just remove the fields from CMB 2.0 and allow them to enter their terms in the metaboxes provided. However, since that allows them to enter as many terms as they want (and create new terms), we needed to remove that metabox.

The register_taxonomy() function has several arguments for showing the taxonomy in various parts of the dashboard, and I thought that the show_ui argument may help, but this also removed the taxonomy from the left-hand menu, meaning that even admins couldn’t (without typing in the URL manually) moderate and edit the taxonomy.

As it turns out, in WordPress 3.8, new functionality was added that allowed developers to specify a callback function for the taxonomy metabox, so that they could provide their own alternate metabox for the taxonomy. Since this argument defaults to either post_categories_meta_box() or post_tags_meta_box() (depending on whether the taxonomy is hierarchial or not) when it is not defined, it has the side benefit of being able to be set to false to stop it from providing the metabox at all.

As such, you can set meta_box_cb to false in your args and you’ll be all set. See line 30 in this example:

Now you still have the taxonomies in your menu, but they won’t appear in your post editor so that you can have more control over how they’re used.


4 thoughts on “Hide a taxonomy metabox in the post editor”

  1. Piet says:

    That’s a very handy tip, thanks for sharing. I guess to relieve the website owner from the work of filling in the Vendor Type and Areas Served, it can be an idea to give the Vendor the option to fill in one of each by making the data available as a custom meta box drop down.

    1. Exactly, and that’s how I handled it (using CMB2 to create and manage the metaboxes).

  2. Piet says:

    I am using the Meta Box plugin to create custom meta boxes (managing I do myself) and it also has the option to add taxonomy terms to a select dropdown. Using it now for post codes and it works awesome. Again thanks for the tip on hiding it from the sidebar!

  3. Dawesi says:

    I use pods framework to achieve the same thing through just a gui

Leave a Reply