Support

Account

Home Forums ACF PRO User Field (Relational), bug when updating field using update_field()

Solving

User Field (Relational), bug when updating field using update_field()

  • ACF PRO version 5.8.6, WP version 5.2.4.

    I’ve created field “Speakers”:

    • Field type – “User”
    • Allow Null? – Yes
    • Select multiple values? – Yes
    • Return format – User ID

    When I’m updating field in admin (two users added), everything works correctly:

    – DB value:
    a:2:{i:0;s:1:"1";i:1;s:3:"101";}

    – field admin HTML:

    <input type="hidden" id="acf-field_5ce2cd297ade8-input" name="acf[field_5ce2cd297ade8]"/>
    <select
        id="acf-field_5ce2cd297ade8"
        class=""
        name="acf[field_5ce2cd297ade8][]"
        data-ui="1"
        data-ajax="1"
        data-multiple="1"
        data-placeholder="Select"
        data-allow_null="1"
        multiple="multiple"
        size="1"
    >
        <option value="1" selected="selected" data-i="0">master (Test Test)</option>
        <option value="101" selected="selected" data-i="1">robot</option>
    </select>

    – UI shows two users:

    BUT, when field is update using update_field(), admin UI shows empty value.

    – PHP code:
    update_field( 'speakers', array( 1, 101 ), $post_id );

    – DB value (same as updating field in admin!):
    a:2:{i:0;s:1:"1";i:1;s:3:"101";}

    – field admin HTML (same as updating field in admin!):

    <input type="hidden" id="acf-field_5ce2cd297ade8-input" name="acf[field_5ce2cd297ade8]"/>
    <select
        id="acf-field_5ce2cd297ade8"
        class=""
        name="acf[field_5ce2cd297ade8][]"
        data-ui="1"
        data-ajax="1"
        data-multiple="1"
        data-placeholder="Select"
        data-allow_null="1"
        multiple="multiple"
        size="1"
    >
        <option value="1" selected="selected" data-i="0">master (Test Test)</option>
        <option value="101" selected="selected" data-i="1">robot</option>
    </select>

    – UI empty:

    Is it bug, or am I missing something?

  • BTW, in both cases
    get_field( 'speakers', $post_id );
    returns
    Array ( [0] => 1 [1] => 101 )

  • Are you attempting to update the field for a post that does not have a value previously set using the admin?

    If this is the case then you need to use the field key instead of the field name when calling update_field().

  • Yes, I did test with update_field() using field key – same outcome.

  • I’m actually stumped because I don’t see any reason why it shouldn’t be working.

  • Hello,
    Did you find why this was happening?
    I have the exact same issue, in the admin the select seems to work since the option has the selected attribute:

    <select id="acf-field_5e78f48412b5e" class="select2-hidden-accessible" name="acf[field_5e78f48412b5e][]" data-ui="1" data-multiple="1" data-placeholder="Choisir" data-allow_null="0" multiple="" size="1" tabindex="-1" aria-hidden="true">
    <option value="3" selected="selected" data-i="0">jason.g@….ch</option>
    </select>

    But the select2 doesn’t work, and when saving again from the admin, it resets the value.

    Thank you,
    Jason

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘User Field (Relational), bug when updating field using update_field()’ is closed to new replies.