Description
As of version 1.2, CPT-onomies allows you to exclude specific CPT-onomy term(s) from being assigned by hooking into a filter. This filter, ‘custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids’, is applied when the ‘Assigning CPT-onomy Terms’ meta box is printed on the “edit post” page, and therefore excludes the term(s) from being listed, and whenever $cpt_onomy->wp_set_object_terms() is invoked, which ultimately keeps the term(s) from being assigned.
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.
FYI: A CPT-onomy term’s term ID is the same as its post ID.
To exclude a term, 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 ’4′ is required because this filter includes four parameters. Be sure to remember that filters must always return the value you’re filtering!
Examples
<?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
- Added in version 1.2
