Support

Account

Home Forums General Issues update ACF field for user

Solved

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!

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

You must be logged in to reply to this topic.