Home Forums ACF PRO Saving to wp_term_relationships table from taxonomy field on user profile


Saving to wp_term_relationships table from taxonomy field on user profile

  • Hi all,

    I would really appreaciate some advice on the best way to resolve a problem with the Taxonomy Field Type when applied to Users.

    I have categories and custom taxonomies set up as taxonomy fields with the field group set to appear on user profiles. The taxonomy field type is checkbox with allow multiple select, Load and Save options set to Yes.

    After editing a user profile ‘John Doe’ and selecting terms from the taxonomies the term_ids are correctly stored as a serialized array in the wp_usermeta table. However the entries that are created in the wp_term_relationships table have object_id = 0, rather than the user_id for ‘John Doe’. When I edit another user profile ‘Fred Bloggs’ the terms applied to ‘John Doe’ are loading in the taxonomy field. Applying new terms to ‘Fred’ shows that the terms simply accumulate.

    I can see that ACF only sets post_ids (not user_id) as the object_id when it performs wp_set_object_terms() acf/fields/taxonomy.php. I assume that this is to avoid a potential data conflict issue where a post_id and user_id are the same.

    To help avoid the conflict I have emptied my database set the autoincrement value on the wp_users table to 1000000 to reserve ids 1-1000000 for posts and 1000000+ for users.

    Can anyone suggest some code that would enable me to hook into the ACF save terms process and allow user_ids perhaps using

    wp_set_object_terms( $user_id, $term_ids, $taxonomy, false );

    I have adapted the following suggested at for use in functions.php.

    function save_usermeta_as_tax ($user_id) {
        $term_ids = get_field('field_xxxxxxxxxxxxx', 'user_'.$user_id);
        wp_set_object_terms( $user_id, $ids, 'category', false);
        clean_object_term_cache( $user_id, 'category' );
    add_action('personal_options_update', 'save_usermeta_as_tax', 10);

    This does save the user_id as object_id and term_id as term_taxonomy_id in the wp_term_relationships table. For some reason it only works if user profile is saved twice.

    Lets say I have applied 2 categories / terms with term_ids 12 and 16 to user_id of 1. This is what the wp_term_relationships table now shows

    object_id 	term_taxonomy_id 	term_order 
    1		12			0 
    1		16			0 

    The new problem that this reveals is that when you go to edit the user with user_id = 1, ACF does not load the values from the wp_term_relationships table in the taxonomy field because it is looking for a post_id of 1 not a user_id of 1. Now I also need some code that would enable me to hook into the ACF load terms process and allow user_ids as well as post_ids.

    It looks as if I am trying to make WP and ACF achieve things that are outside their ‘core’ scope. Perhaps the only viable alternative is to create a custom post type (e.g. member profile) to hold the user profile data. I guess this could be generated after successful user registration and have the user assigned as the author.

    Any thoughts most welcome 😉

  • ACF does not support user taxonomies and from what I understand the developer will not add it since WP does not really support user taxonomies.

    There has been some discussion on these forums about this and ways to get it working. Not sure if this will help you or not.

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

The topic ‘Saving to wp_term_relationships table from taxonomy field on user profile’ is closed to new replies.