Support

Account

Home Forums Backend Issues (wp-admin) Multiple Select value not saved

Solved

Multiple Select value not saved

  • Hello,

    I created a custom field type with a multiple values select as you can see down below : `function render_field( $field ) {
    $current_user = wp_get_current_user();
    $field = array_merge($this->defaults, $field);
    ?>
    <div>
    <select name='<?php echo $field[‘name’] ?>’ id=”select_list_formateur” class=”chosen-select” multiple=”multiple”>
    <?php
    foreach( $this->get_formateurs() as $formateur ) :
    ?>
    <option <?php selected( $field[‘value’], $formateur[‘id’] ) ?> value='<?php echo $formateur[‘id’] ?>’><?php echo $formateur[‘first_name’].’ ‘.$formateur[‘last_name’] ?>
    </option>
    <?php endforeach; ?>
    </select>
    </div>
    <?php
    }`

    The field works I have everything I need but when I update my post only one value of the select is stocked.

    Did I miss something ? Should I use the update_value() ? I’m lost and in need of your help.

    Please excuse my broken english.

  • You should use update_field and pass all the selected values in an array

    // save a checkbox or select value
    $field_key = "field_1234567";
    $value = array("red", "blue", "yellow");
    update_field( $field_key, $value, $post_id );

    https://www.advancedcustomfields.com/resources/update_field/

  • Thank you Jdorner but when I var_dump the $_POST before updating my post my field has only one value instead of an array with the multiple values selected before.

    In your exemple you write in hard code the values before updating the field but I cant.

    I might have poorly explain my problem and I’m sorry for that.

    I created a multiple values select field but when I update my post only one value is saved in the DB. I wrote a tiny script to see the value of the select on change and when I selected two or three values, the select values are good. But when I update my post, the value in $_POST is not correct, there’s only the first value selected.

    I’m sorry it’s a little bit confusing but I dont really know what to say.

  • I found the solution, I just had to add a little ‘[]’ at the end of my select name

    
    <select name='<?php echo $field[‘name’] ?>[]’ id=”select_list_formateur” class=”chosen-select” multiple=”multiple”>
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Multiple Select value not saved’ is closed to new replies.