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;
}
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.