Support

Account

Home Forums ACF PRO Set radio button to selected

Solved

Set radio button to selected

  • Hey there

    So what I want to do is set a radio button to selected from the backend in functions.php. I have a ACF field with two options – ‘blue‘ which is selected by default and ‘red‘ which is unselected

    So I’ve got this function in functions.php and I’m trying to update the choices array but I’m not having much luck …

    I can reset all the choices set in my array etc so I know I’ve got the right field etc and close to getting this but I’m missing that last piece of the puzzle

    function acf_load_colours_field_choices( $field ) {
        
        if( is_array($choices) ) {
            
            foreach( $choices as $choice ) {
    			$field['choices'][ $choice ] = $choice;
    			update_field('field_1234567890', checked );
            }
            
        }
        
        return $field;
        
    }
    
    add_filter('acf/load_field/name=user_level', 'acf_load_colours_field_choices');

    So what I’ve tried to do is update the field ‘field_1234567890‘ and set it as checked but that isn’t working and I’m not sure how else to do it … I’ve gone off the fields key but happy to go off value etc if that makes more sense

    If someone can point me in the right direction on this one I’d be hugely grateful!

  • If I understand correctly your field has 2 options
    1) red
    2) blue

    If you update the field with the value checked, to which value should it apply ?
    You haven’t provided a real value which should be stored, only a string which does not occur in your options.

    And… checked is only used by checkboxes (afaik).
    In this case you would have to actually set the value of what you want to save.

    So you would have to save red or blue (assuming those are both the id and value).

    So I think you would have to do something along the line of:

    update_field('field_1234567890', 'red' );
    update_field('field_1234567890', 'blue' );
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.