Changing the CPT-onomy Meta Box Format

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;
}

?>