Home › Forums › Backend Issues (wp-admin) › Use radio buttons to assign custom taxonomy slug to custom post type › Reply To: Use radio buttons to assign custom taxonomy slug to custom post type
I was wrong, in your loop you need to check if it’s the current post, if it is you set the term and if it’s not you clear the term, like you’re doing. Since you’re using a radio field it does not get set automatically. You also need to declare the global $post, and you need to do wp_reset_postdata after your loop. something like this.
<?php
function my_acf_save_post($post_id) {
global $post;
// get the value of the radio button that is selected
$value = get_field('is_featured_video');
// only run the code, if the user has selected "Yes" (yes_featured_video)
if($value == 'yes_featured_video') {
// start by fetching the terms for the featured_video taxonomy (there is only one, which is 'yes')
$terms = get_terms( 'featured_video' );
if(!empty($terms)) {
// now run a query for each term
foreach ( $terms as $term ) {
$args = array(
'post_type' => 'asc_video',
'featured_video' => $term->slug
);
$query = new WP_Query( $args );
}
// start the loop
while ( $query->have_posts() ) : $query->the_post();
// remove the slug from the video that previously had the 'yes' slug assigned to it
if ($post_id != $post->ID) {
wp_set_object_terms( get_the_ID(), '', 'featured_video');
// set the radio button value to "No" (no_featured video)
update_field('is_featured_video', 'no_featured_video');
} else {
wp_set_object_terms($post_id, $term, $term->taxonomy);
}
endwhile;
wp_reset_postdata();
}
// assign the 'yes' slug to the post
wp_set_object_terms($post_id, 'yes', 'featured_video');
}
}
add_action('acf/save_post', 'my_acf_save_post');
?>
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.