I need a solution to provide a single True/False field with the ability to activate a special taxomy term in the backend of a custom post type entry.
Thanks in advance for any help!
I don’t have any code examples.
What you need to do is create an acf/save_post action. In this action you check the value of the true/false field. If it is true then you use wp_set_post_terms() to set the term.
You will also need to check to see if the value of the true/false field is false and if the term is set for the post (wp_get_post_terms()) and again use wp_set_post_terms() to remove it.
Thank you @hube2 , that’s completely true!
In the meantime I found a solution that works for me. If anyone else needs it:
function update_post_taxonomy( $post_id ) {
$myfield = get_field('my_field_name');
if( $myfield ) {
wp_set_object_terms($post_id, 'myterm', 'mytaxonomy', true);
}
add_action('acf/save_post', 'update_post_taxonomy', 20);
Cheers!