Hi,
Say I have a CPT called "Hobbies", assigned to it a custom taxonomy called "Musical Instruments".
I want to create a custom field group called "musical instruments information", where these are fields for:
- Length
- Width
- Weight
- Number of strings...etc
all this is easy to do..
However, I want to control the custom field "Number of strings" to ONLY show when the custom Taxonomy "Musical Instruments" has specific terms (e.g. guitar or violin) this is because this field is useless when the "drums" or "flute" are chosen.
I tried to specify conditions for the the field, but the "basic" method only has "custom fields", and I could not create a manual expression to do so.
so is there a way that I can specify when a custom field shows in the backend, based on a specific taxonomy chosen for the post that the filed is in?
Hello. Thank you for contacting the Toolset support.
Well - that is desired behavior that you can not add conditional display of custom field of post field group based on taxonomy term. There is no such feature exists.
But I've workaround to offer you.
1) do not make your field required.
2) write your own custom script to achieve your goal using jquery or custom JS
3) Or this following way you may partially achieve it on post edit page:
You need to use WordPress hook "admin_head" to remove the specific metabox based on your condition. You should remove "Number of strings" custom field from your existing group and create a NEW post field group and assign the field to this group and also assign this group to your "hobby" CPT.
Please add following code to your current theme's funcitons.php file.
function custom_remove_post_metabox() {
global $post_type;
global $post;
global $current_user;
if($post_type=='hobby'){
remove_meta_box( 'wpcf-group-{YOUR-POST-FIELDGROUP-SLUG}', $post_type,'normal');
}
}
add_action('admin_head', 'custom_remove_post_metabox' );
Where:
- Replace '{YOUR-POST-FIELDGROUP-SLUG}' with your original custom fields group slug.
- You should adjust the above code as required. This is just to give you hint how you can hide the meta box.
I hope above solution will help you to resolve your issue.