Changing the CPT-onomy Meta Box Format

You have been redirected from rachelcarden.com to my new home at WP Dreamer! I moved all my content over so you should be able to find everything you're looking for. If not, please let me know!

As of version 1.3, you can customize your term selection format via the settings page.

Assigning your CPT-onomy terms is no different than assigning taxonomy terms. Meta boxes will be added to each “edit post” page, where applicable, so users who have the capability can select, or “assign”, the desired terms.

There are three different formats for CPT-onomy term selection:

  • a checklist – which is the default for hierarchical CPT-onomies
  • an autocomplete box – which is the default for non-hierarchical CPT-onomies
  • and a select dropdown – if you only want one term to be selected

If you want to change your term selection format

If you have a hierarchical CPT-onomy but want an autocomplete box instead of a checklist, or if you want to use the select dropdown, CPT-onomies allows you to hook into the meta box (via a filter) and overwrite the default selection format.

The filter provides the name of the CPT-onomy and the object’s post type, allowing you to drill down your customization as far as you like. You can change the format for a select CPT-onomy, or for a select post type, or you can change the format for a select CPT-onomy but only when you’re editing a select post type.

To filter the format, tweak the code below (to fit your CPT-onomies) and add it to your functions.php file. In the add_filter() declaration, the 1 stands for your filter’s priority (and can be adjusted) but the 3 is required because this filter includes three parameters.

Return ‘checklist’ for the checklist, ‘autocomplete’ for the autocomplete box and ‘dropdown’ for the select dropdown. Be sure to remember that filters must always return the value you’re filtering!

Some need-to-knows about filters:

  • To use this filter, tweak the code below and add it to your functions.php file.
  • If a filter accepts more than one argument, you have to declare the number of arguments in your add_filter() declaration, along with the filter’s priority.
  • All filters MUST return a value, i.e. the value being filtered.
  • For more information, check out add_filter() in the WordPress codex.
<?php

add_filter( 'custom_post_type_onomies_meta_box_format', 'my_website_custom_post_type_onomies_meta_box_format', 1, 3 );
function my_website_custom_post_type_onomies_meta_box_format( $format, $taxonomy, $post_type ) {
   // when editing a post with the post type 'movies',
   // we want to assign the 'actors' CPT-onomy terms with an autocomplete box
   if ( $post_type == 'movies' && $taxonomy == 'actors' )
      return 'autocomplete';
   // no matter the post type, we want to assign the 'actors' CPT-onomy terms with a select dropdown
   elseif ( $taxonomy == 'actors' )
      return 'dropdown';
   // no matter the post type, we want to assign the 'directors' CPT-onomy terms with a checklist
   elseif ( $taxonomy == 'directors' )
      return 'checklist';
   // WordPress filters must always return a value
   return $format;
}

?>

Related Functions

4 Comments

  • Goose says:

    Hi Rachel – thanks for this post. I’m not a techie but do have an issue and found your page. I have a regular taxonomy box that I use to assign taxonomy terms to posts. It is a hierarchical taxonomy. I understand that these default to the checkbox format, but I would like to be able to use the autocomplete functionality. Do you know of a way to do this? In reading your post (with my basic knowledge) it looks like you are doing this for the plug-in you created which relates to custom post types. Is there a way to do this with just regular taxonomies? I searched Google and the Plug-in Forums but wasn’t able to find something that would work. An alternative would be to figure out a way to make my taxonomy meta box longer – but I couldn’t figure out how to do that either! Thanks for all you do for the greater WordPress community.

    • rachelcarden says:

      Hi Goose. Yes, this functionality was created just for my plugin and, sorry, but I don’t know how to do this with a regular taxonomy and I don’t know of any plugins. I searched the repository for “autocomplete taxonomy” and found a plugin called “Simple Tags” but it doesn’t look very promising. You can make your taxonomy meta box longer by adding some CSS styles to the page to change the height of the box, if you like.

  • Adam Robertson says:

    Rachel, really great work on this plugin. It is a brilliant piece of work. I am already using it on a couple of projects.

    On one site, I am using a drop-down meta-box for the cpt-taxonomies; however, the particular drop-down is in danger of becoming rather long over time.
    Currently the meta-box lists the cpt-onomies aphabetically. I would like to change this to list by date, which will allow for the most recent to be at the top of the list. I have not found the filter to do this yet, but I will keep looking through your documentation.
    Meanwhile, if you read this comment, and know the answer, could you give me nudge in the right direction.

    I think I am looking for ksort(__)

    • rachelcarden says:

      I don’t have a filter inside CPT-onomies for such a tweak BUT it uses get_terms() to retrieve the functions so you could filter ‘get_terms_args’ inside that function to change the order. Just be sure to put some logic in your filter so it’s only doing it in the admin on that specific page because there’s really no way to detect you’re filtering inside the meta box. Hope that helps!

Leave a Reply