Home Forums General Issues Disable Options for Select Field Reply To: Disable Options for Select Field

  • Looking at the OP.

    You want to remove some values for new entries, but allow them on current entries, or when an admin is editing. I would simply remove them from the front end of the site or based on other things. Removing an choice in a select field will not remove that value where it has already been selected.

    add_filter('acf/prepare_field/name=select_field_name', 'filter_choices', 20);
    function filter_choices($field) {
      if (is_user_logged_in() && current_user_can('administrator')) {
        // not for admins
        return $field;
      // values to remove
      $remove = array(1, 2, 3);
      // get the current value of the field
      $value = acf_get_array($field['value']);
      // get all field choices
      $choices = field['choices'];
      // clear the field choices
      $field['choices'] = array();
      foreach ($choices as $v => $l) {
        if (!in_array($v, $remove) || in_array($v, $value)) {
          // this value should not be removed
          // or it might have been previously selected
          // so keep it
          $field['choices'][$v] = $l;
        } // end if keep it
      } // end foreach choices
      return $field;