Home › Forums › ACF PRO › Update taxonomy field in user profile › Reply To: Update taxonomy field in user profile
@fountain I can confirm it works. However, after you update the field, you also have to manually attach the user id to the terms ids in the wp_term_relationship table using wp_set_object_terms(). Otherwise the terms ID’s will be meaningless. WordPress won’t understand those terms are associated with the user.
Also, I’m not sure but you may need to pass the updated field value as an integer and not a string. Maybe also as an array. I’m not sure what update_field checks for.
In my case, I have several terms that I need to associate with each user.
Here’s what I’m using and it works. Keep in mind I’m hooking into gform_user_registered so I can access data submitted through the Gravity Forms user addon form ($feed and $entry, respectively). Sorry for the sloppy comments below, I’m in a bit of a hurry:
add_action( 'gform_user_registered', 'wda_add_expertise', 10, 4 );
function wda_add_expertise($user_id, $feed, $entry) {
$terms = rgar( $entry, '7' ); //get Gravity Forms multiselect field values.
if ($terms) {
// An array of IDs of categories we want this user to have.
$cat_ids = json_decode($terms); // must convert Gravity Forms json to array of integers
// Update ACF
update_field('ares_of_expertise', $cat_ids, 'user_' . $user_id);
// Connect terms with user
/*
* If this was coming from the database or another source, we would need to make sure
* these were integers:
*/
$cat_ids = array_map( 'intval', $cat_ids );
$cat_ids = array_unique( $cat_ids );
// Connecting taxonomy ID's for custom taxonomy "expert" to user ID
wp_set_object_terms( $user_id, $cat_ids, 'expert' );
}
}
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.