Home › Forums › General Issues › update ACF field for user
I am trying to update an advanced custom fields user field but it doesn’t work ie: the value is not updated. I get no error either. It is a dropdown/select field. Not sure if this makes a difference.
$email = $_POST['email'];
$user_data = get_user_by('email', $email);
$user_id = $user_data->ID;
update_field( 'location', $_POST['current_country'], 'user_'.$user_id );
// I also tried with field key
//update_field( 'field_5fb3785065726', $_POST['current_country'], 'user_'.$user_id );
If the ACF field does not already exist, i.e. you are updating a field that previously has no value, then you need to use the field key. See the doc here https://www.advancedcustomfields.com/resources/update_field/
If the ACF field does not already exist, i.e. you are updating a field that previously has no value, then you need to use the field key. See the doc here https://www.advancedcustomfields.com/resources/update_field/ five nights at freddy’s
Thanks man. That is what I was needing
Thanks for getting back to me!I’ve worked with lots of different CRM systems, including projects where we’ve been a Salesforce implementation partner, so I’d like to share some more info about working with ACF fields for users.
When you update a custom ACF field, there are two ways to do it: use field_name or field_key.Field_name only works if the field already exists and has a value.For example:
update_field('location', $_POST['current_country'], 'user_'.$user_id);
Field_key is a more reliable way, especially if the field may not exist or doesn’t have a value.For example:
update_field('field_5fb3785065726', $_POST['current_country'], 'user_'.$user_id);
This will help avoid problems with updating fields that may not exist.I hope this will be useful to other developers!
You must be logged in to reply to this topic.
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.