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.

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.