Support

Account

Home Forums General Issues Setting ACF true/false field value automatically Reply To: Setting ACF true/false field value automatically

  • Create an acf/prepare_field filter

    You will need the field key of this filter. I don’t have all the code

    
    <?php 
    add_filter('acf/prepare_field/key=FIELD_KEY_HERE', 'FUNCTION_NAME_HERE');
    function FUNCTION_NAME_HERE($field) {
      if (is_admin()) {
        return $field;
      }
      // get the current user level/role and check
      if ($role_can_create_featured) {
        // if the user is able to create a featured post
        // set the value default value to tru
        $field['default_value'] = 1;
        return $field;
      } else{
        // user not allowed to create featured post
        // create a hidden field that matches the acf true false field
        ?><input type="hidden" name="acf[FIELD_KEY_HERE]" value="0" /><?php
        // remove the existing field
        return false;
      }
    }