I would like to use this function to activate my taxonomies via a true/false field. And it works perfectly in the backend.
function update_post_taxonomy( $post_id ) {
$myfield = get_field('my_field_name');
if( $myfield ) {
wp_set_object_terms($post_id, 'myterm', 'mytaxonomy', true);
} else if ( empty( $myfield ) ) {
wp_remove_object_terms( $post_id, 'myterm', 'mytaxonomy', true );
}
}
add_action( 'acf/save_post', 'update_post_taxonomy', 10 );
But unfortunately not in the frontend via acf_form()
.
Could someone help me here? Thanks!
And once again I answer myself 😉
I forgot the $post_id
inside the acf-field. This is how it works perfectly:
function update_post_taxonomy( $post_id ) {
$myfield = get_field('my_field_name', $post_id);
if( $myfield ) {
wp_set_object_terms($post_id, 'myterm', 'mytaxonomy', true);
} else if ( empty( $myfield ) ) {
wp_remove_object_terms( $post_id, 'myterm', 'mytaxonomy', true );
}
}
add_action( 'acf/save_post', 'update_post_taxonomy', 10 );