Support

Account

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.