Home › Forums › ACF PRO › There can only be one field checked › Reply To: There can only be one field checked
This did the job with a true or false field, please tell me if the code is very poor or if the solution is valid.
function update_featured_post( $post_id ) {
// Get current id of post being edited
$currentID = get_the_ID();
// Get all posts except current post
$posts = get_posts([
'post_type' => 'post',
'post__not_in' => [$currentID],
]);
// Get ACF true or false value
$value = get_field('post_em_destaque');
// Find if other post is marked as featured
if( $value = true ) {
foreach( $posts as $p ) {
// Uncheck field if checked
update_field('post_em_destaque', false, $p->ID);
}
}
}
add_action('acf/save_post', 'update_featured_post', 20);
Thank you for your patient, hope someone find this code useful.
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.