Support

Account

Home Forums General Issues How to update acf field value for wp user

Solving

How to update acf field value for wp user

  • Hello-

    I am using Ultimate Member and have a custom post type for our subscribers called Companies. Whenever a user subscribes, we have to go in and approve the membership and assign them to a company via ACF relationship field.

    I have this code below but it doesn’t seem to be working. Does anyone know how I need to edit the code to update the ACF field value?

    add_action( 'um_user_register', 'my_user_register', 10, 2 );
    function update_user_company( $user_id, $args ) {
        $user = get_user_by( 'ID', $user_id );
    		$email = $user->user_email;
    		list($user, $domain) = explode('@', $args['user_email'] );
    		if ($domain == 'gmail.com') {
    				add_user_meta( $user_id, 'user_company', 'Google');
    		};
    		if ($domain == 'pearlsinarts.com') {
    				add_user_meta( $user_id, 'user_company', 'Pearlsin Arts');
    		};
    }
    
  • You need to use the ACF function update_field() and you need to use the field_key to update.

    https://www.advancedcustomfields.com/resources/acf-update_field/

    When using a relationship field or a post object field then the value of the field will be post ID or an array of post ID representing the company post that is related. For a relationship field this will always be an array. For a post object it will be a single value unless you have enabled multiple selections and then it will be an array.

  • Thank you, I will try to see if I can get this. I get the gist of it but it’s writing the function that confuses me as I’m still new to this.

  • @hube2 I tried this but I can’t get it to work and I am not exactly sure how to go about writing this. Can you give me some pointers?

    add_action( 'profile_update', 'update_user_company' );
    add_action( 'personal_options_update',  'update_user_company' );
    add_action( 'edit_user_profile_update', 'update_user_company' );
    function update_user_company( $user_id, $args ) {
        $user = get_user_by( 'ID', $user_id );
    		$email = $user->user_email;
    		$field_key = 'user_company';
    		list($user, $domain) = explode('@', $args['user_email'] );
    		if ($domain == 'amyling.com') {
    			$company = $user_company->post_title('Pearlsin Arts');
    			update_field($field_key, $company, 'user_' . $user_id);
    		};
    }
  • I also tried this but doesn’t work. when I update the code in functions.php, I just go to the user’s profile in the backend and refresh the page to see if the relationship field has been updated but it stays on “Select”.

    add_action( 'profile_update', 'update_user_company' );
    add_action( 'personal_options_update',  'update_user_company' );
    add_action( 'edit_user_profile_update', 'update_user_company' );
    function update_user_company( $user_id, $args ) {
    	$user = get_user_by( 'ID', $user_id );
    	$email = $user->user_email;
    	$company = get_field( 'user_company', 'user_' . $user_id );
    	if ($company == '') {
    		list($user, $domain) = explode('@', $args['user_email'] );
    		if ($domain == 'amyling.com') {
    			update_field( 'user_company', 'Pearlsin Arts');
    		};
    	}
    }
  • You would have to do a query to get the correct post ID for the “company” post.

    The relationship field holds ID values, not post titles.

    This means you need to do a query of the company posts to get the post that has the title you want and then use the ID form the post that you find to update the users company field.

  • @hube2 I looked at the ACF field again and noticed that user_company is actually a post object field type with a return format of post object.

    How would I check if the field is empty/null before I do update_field? Would I also still need do a query of the company post?

  • It does not matter what the return format of the field is. A post object field always holds the post ID of the related post or an array of post IDs if the field allows multiple selections. Return format has no bearing on values stored in the database and you must use the values as ACF stores them in the DB when updating a field.

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

You must be logged in to reply to this topic.