Support

Account

Home Forums General Issues update_field for user data with array Reply To: update_field for user data with array

  • For others stuck on this I have a solution.

    I tried all sorts of combinations of (examples)
    update_user_meta(36, 'delivery_partner', '');
    or
    update_field('delivery_partner', $a, 'user_36');

    but nothing worked.. then I noticed that the issue is because my options are different key => values. ie:

    'value'	=> 7,
    'label'	=> 'darren'

    what I need to do is just update the ‘value’ and the label automatically populates correctly… I tested several times.. really unexpected behaviour 🙂

    So the solution is:
    update_user_meta(36, 'delivery_partner', 13);
    36 is the user id
    ‘delivery_partner’ is the field name
    13 is the ‘value’

    The result from
    $g = get_field('delivery_partner', 'user_36');
    is:
    Array ( [value] => 7 [label] => darren )

    However..
    I also realised that this solution only works if the options have been created already. In my case I was using a function to dynamically populate the options on a user edit page.

    This meant that if I had not yet visited the user’s admin page (like if im creating a new user), then the above solution wont work and you need to add both ‘value’ and ‘label’ directly with:

    update_field('delivery_partner', $partner->ID, 'user_' . $user->ID);
    Note that update_field adds both value and label by default in my case.