Skip Navigation

[Resolved] How to display a custom field in backend based on the current post's taxonomy?

This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Author
Posts
#602151

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?

Thanks.

#602194

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

More info:
https://codex.wordpress.org/Function_Reference/remove_meta_box

If you need custom programming work for your site, please feel free to contact our certified partners:
=> https://toolset.com/consultant/