Home › Forums › General Issues › update_field for user data with array
Hello,
I’ve been using ACF for years now and love your plugin.. and the doc’s are always very helpful.
However im stuck on something regarding updating a select field.
– I have the acf select field: delivery_partner.
– The field appears on the user profile and has array balues like:
7 => ‘darren’
10 => ‘steve’
– I can select the option in the user admin, the value is saved correctly and appears selected on page reload.
How do I update this field in php?
I tried:
$new = array(
‘value’ => 7,
‘label’ => ‘darren’,
);
update_field(‘delivery_partner’, $new, ‘user_120163’);
But now if I print_r the field I get:
‘
Array
(
[value] => Array
(
[value] => 7
[label] => darren
)
[label] => Array
(
[value] => darren
[label] => darren
)
)
‘
Can you see what im doing wrong?
thanks
Darren
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.
The topic ‘update_field for user data with array’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.