Support

Account

Home Forums ACF PRO Update taxonomy field in user profile

Solving

Update taxonomy field in user profile

  • Hello,

    I’m trying to update a ACF Taxonomy-Field (which is attached to the WP standard category-taxonomy) in a user profile. I want that whenever a WP user ist created a certain term in the taxonmy is checked.

    add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
    
    function myplugin_registration_save( $user_id ) {
    
           //where can i set the cat id?
            update_field( 'allowed_cats',"1",'user_'.$user_id);
    
    }
    

    or should I try to preselct the value:
    https://support.advancedcustomfields.com/forums/topic/default-value-for-the-taxonomy-field-type/ – but this seems only to work on posts not on user attached fields?

    • JMay

    • October 28, 2017 at 10:43 am

    @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' ); 
    
     }
    
    }
    • JMay

    • October 28, 2017 at 10:51 am

    Also, thank you. I didn’t realize I needed to concatenate ‘user_’ to $user_id for update_field(). That was the last bit I needed for my field update to work.

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.