Home › Forums › Feature Requests › Conditional Logic using Taxonomy field › Reply To: Conditional Logic using Taxonomy field
Hi, I seem to be having a bit of a problem. I used Beee’s code and altered it to fit my taxonomies, but unfortunately it’s not working as wanted.
I have a custom post type with a taxonomy (‘type’), type can either be ‘movie’ or ‘series’.
My ACF Radio button field has these choices, the value given is the value of the term:
2 : Movie
4 : Series
This will output the value of the field, not the name or array.
In my functions.php I have:
function change_media_type( $post_id ) {
// bail if no ACF data
if ( empty($_POST['acf']) ) {
return;
}
// get term id from $post_id (only 1 value is allowed, so it returns 1 value only)
$stored_type = wp_get_post_terms($post_id, 'type');
// get submitted value from acf form
$posted_type = $_POST['acf']['field_picktype'];
// get term_id for the submitted value
$term_id = get_term_by( 'id', $posted_type, 'type' );
// if stored value is not equal to posted value, then update terms
if ( $stored_type != $posted_type ) {
wp_set_object_terms( $post_id, $term_id->term_id, 'type' );
}
}
add_action('acf/save_post', 'change_media_type', 20);
But it doesn’t seem to change the taxonomy of the post as I want. It changes, but simply changes to “—”. Could anyone point me in the right direction?
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.