This question is possibly a duplicate of: Only show metabox when date-value in other metabox is over?
Broadly there are two ways of achieving this. Using the add_meta_boxes
hook to pass two variables: post type and the post. Then add the metabox only when the post has a certain term:
add_action( 'add_meta_boxes', 'myplugin_add_my_custom_box',10,2);
function myplugin_add_my_custom_box($post_type,$post){
//Check if post has 'car' term from 'my-tax' taxonomy. If not don't add metabox.
if( ! has_term( 'car', 'my-tax', $post ) )
return
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_metabox_callback',
'event'
);
}
The benefit of this approach is that its more stable – doesn’t rely on javascript and is less ‘dirty’ than simply hiding the metabox which is what the second option is. The benefit of using javascript is that you can make it ‘live’ (so adding the term ‘car’ makes the term appear, removing it makes it disappear).
Note: When hiding you will want to clear any values so that the hidden meta box doesn’t post any data that you or WordPress then saves.
This method is described here: Toggle admin metabox based upon chosen page template