Including/Excluding CPT-onomy Terms

CPT-onomies 1.2 (excluding) and 1.3 (including) introduced two new filters and the functionality to ‘include’ and ‘exclude’ CPT-onomy terms when being assigned. These filters are applied when the ‘Assigning CPT-onomy Terms’ meta box is printed on the ‘Edit Post’ page and whenever $cpt_onomy->wp_set_object_terms() is invoked.

To use either filter, tweak the code provided (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 4 is required because this filter includes four parameters. Be sure to remember that filters must always return the value you’re filtering!

Don’t forget that a CPT-onomy’s term ID is the same as it’s custom post type post ID!

Including CPT-onomy Terms

As of version 1.3, CPT-onomies allows you to designate that you only want specific CPT-onomy term(s) to be assigned by hooking into the custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids filter and returning the term ID(s) of the terms you want included. Including terms means you only want these specific terms to be available for assignment.

This filter provides the name of the CPT-onomy and the object’s post type and post ID, allowing you to drill down your inclusions as far as you like by simply including the term ID(s) in the $include_term_ids array.

<?php
add_filter( 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids', 'my_website_custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids', 1, 4 );
function my_website_custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids( $include_term_ids, $taxonomy, $post_type, $post_id ) {
   switch ( $taxonomy ) {

      case 'actors':
         // we only want these specific actors to be included
         $include_term_ids = array_merge( $include_term_ids, array( 277, 316 ) );
         break;

      case 'directors':
         // if being assigned to the post with this ID, only include this director
         if ( $post_id == 7715 )
            $include_term_ids[] = 286;
         break;

   }
   return $include_term_ids;
}
?>

Excluding CPT-onomy Terms

As of version 1.2, CPT-onomies allows you to exclude specific CPT-onomy term(s) from being assigned by hooking into the custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids filter and returning the term ID(s) of the terms you want excluded. Excluding a term means the term will not be available for assignment.

This filter provides the name of the CPT-onomy and the object’s post type and post ID, allowing you to drill down your exclusions as far as you like by simply including the term ID(s) in the $exclude_term_ids array.

<?php
add_filter( 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids', 'my_website_custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids', 1, 4 );
function my_website_custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids( $exclude_term_ids, $taxonomy, $post_type, $post_id ) {
   switch ( $taxonomy ) {

      case 'actors':
         // only exclude this term if it's being assigned to a 'movie'
         if ( $post_type == 'movies' )
            $exclude_term_ids[] = 7720;
         // always exclude these terms
         $exclude_term_ids = array_merge( $exclude_term_ids, array( 277, 316 ) );
         break;

      case 'directors':
         // only exclude this term if it's being assigned to the post with this ID
         if ( $post_id == 7715 )
            $exclude_term_ids[] = 286;
         break;

   }
   return $exclude_term_ids;
}
?>

Notes

  • custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids added in version 1.2
  • custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids added in version 1.3