Support

Account

Home Forums Backend Issues (wp-admin) Edit checkbox selection – update_field()

Solving

Edit checkbox selection – update_field()

  • I am trying to use update_field to change checkbox selections, but I can’t get it to work. I tried the following, but nothing is updated.

    $newchoices = array ( 'ID' => 4844, 'key' => 'field_576d194037bae', 'label' => 'Progression', 'name' => 'byggeri_progression', 'prefix' => '', 'type' => 'checkbox', 'value' => array ( 0 => 'Checkbox 1', 1 => 'Checkbox 3', 2 => 'Checkbox 5'), 'menu_order' => 29, 'instructions' => '', 'required' => 0, 'id' => '', 'class' => '', 'conditional_logic' => 0, 'parent' => 4814, 'wrapper' => array ( 'width' => '', 'class' => '', 'id' => '', ), '_name' => 'progression', '_input' => '', '_valid' => 1, 'choices' => array ( 'Choice 1' => 'Choice 2', 'Choice 3' => 'Choice 4', 'Chocie 5'), 'default_value' => array ( ), 'layout' => 'vertical', 'toggle' => 0, );
    
    update_field( 'field_576d194037bae', $newchoices, 4877);

    What am I doing wrong?

  • update_field() is used to update the value of a field and not the field settings.

    Look in the ACF code for the function acf_update_field(), it’s located in /api/api-field.php. This is not a documented function. Basically you give it the new field settings and it will be updated in the database.

  • What i am trying to do is changing the value of the field, in the sense that I want to change which checkboxes are selected for a particular post. Isnt update_field() used for that?

    I.e choice 1, 2 and 5 are checked for field named checkbox in post 4788. I would like to change this so choice 3 and 4 are selected instead.

  • The value your including in the call to update_field in incorrect then. What you have there is the field settings for the entire field. Look at the documentation for update_field here https://www.advancedcustomfields.com/resources/update_field/

    What you want will look something like this

    
    update_field('field_0123456789', array(1,2,5), $post_id);
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.