Support

Account

Home Forums Bug Reports Taxonomies not saving for users

Solved

Taxonomies not saving for users

  • Thanks for advanced custom fields, it’s great.
    My taxonomies are not saving for users. I created a “User Meta” group with the rule “Show this field group if [user] is equal to [all]”. I added a taxonomy I created for a custom post type, as well as a taxonomy for a user (using the user taxonomies plugin http://wordpress.org/plugins/user-taxonomies/). When I click on edit user, both taxonomy checkbox sections show up, but when I click in the checkboxes and click update user, the checkboxes go back to being unchecked.
    I tried disabling the user-taxonomies plugin but the same problem occurs.
    This is one of my custom taxonomies:

    
    register_taxonomy('department', 'user', array(
    		// Hierarchical taxonomy (like categories)
    		'hierarchical' => true,
    		// This array of options controls the labels displayed in the WordPress Admin UI
    		'labels' => array(
    			'name' => _x( 'Department', 'taxonomy general name' ),
    			'singular_name' => _x( 'Department', 'taxonomy singular name' ),
    			'search_items' =>  __( 'Search Departments' ),
    			'all_items' => __( 'All Departments' ),
    			'parent_item' => __( 'Parent Department' ),
    			'parent_item_colon' => __( 'Parent Department:' ),
    			'edit_item' => __( 'Edit Department' ),
    			'update_item' => __( 'Update Department' ),
    			'add_new_item' => __( 'Add New Department' ),
    			'new_item_name' => __( 'New Department Name' ),
    			'menu_name' => __( 'Departments' ),
    		),
    		// Control the slugs used for this taxonomy
    		'rewrite' => array(
    			'slug' => 'department',
    			'with_front' => false, 
    			'hierarchical' => true
    		),
    	));
  • Hi @jordan314

    I have not yet tested ACF with the user taxonomy plugins so I can’t guarantee this would work.

    Are you able to use the WP interface for this instead of ACF for now?

  • Hello.

    Any progress on this issue?

    I’m using CPT-onomies (http://wpdreamer.com/plugins/cpt-onomies/) to create CPT/taxonomy combinations and can’t seem to assing taxonomies to users with ACF. CTP-onomies are a big part of my current project.

    Do I need to resort to using a custom term save function with users for now?

    EDIT: it seems CPT-onomies uses its own system for handling taxonomies. it doens’t utilise the default taxonomy tables in the database, but saves them to meta data instead. Nevermind, will need to create my own system anyways it seems.

  • I see this is marked as solved but I don’t think it is. I ran into the same problem, that user taxonomies would not save as taxonomy terms. They saved just fine as user_meta but not when “Load value based on the post’s terms and update the post’s terms on save” is checked. Two changes to the field taxonomy.php seem to take care of it. The update_value needs to consider user_ids like this:

    function update_value( $value, $post_id, $field )
    {
    	// vars
    	if( is_array($value) )
    	{
    		$value = array_filter($value);
    	}
    
    	if( $field['load_save_terms'] )
    	{
    		// Parse values
    		$value = apply_filters( 'acf/parse_types', $value );
    
    		if( strpos( $post_id, 'user' ) !== false ) {
    			$user_id = substr($post_id, 5);
    			wp_set_object_terms( $user_id, $value, $field['taxonomy'], false );
    		} else {
    			wp_set_object_terms( $post_id, $value, $field['taxonomy'], false );
    		}
    
    	}
    
    	return $value;
    }
    

    The load_value function needs to use the more general wp_get_object_terms instead of get_the_terms like this:

    function load_value( $value, $post_id, $field )
    {
    	if( $field['load_save_terms'] )
    	{
    		$value = array();
    
    		$terms = wp_get_object_terms( $post_id, $field['taxonomy'] );
    
    		if( is_array($terms) ){ foreach( $terms as $term ){
    
    			$value[] = $term->term_id;
    
    		}}
    
    	}
    
    	return $value;
    }
    
  • Hi @smohlke

    Thanks for the code, I’ll run some tests and add this in!

    Cheers
    E

  • Hi @smohlke

    Thanks again for the above code.

    After some reviewing, I’m not sure this can be added into the core due to a potential data conflict issue.

    There is a case when taxonomy terms will be saved to object 5. Object 5 can be both a post AND a user. Although this situation is unlikely, it make occur in a large scale web app when importing many entries where the data is connected with taxonomies in a complicated way.

    For example, a user may be connected to a ‘company’ and also a page may be connected to a ‘company’ – in this case, ‘company’ is a taxonomy term.

    When editing the page or the user, the term relationship data would not be unique and would override each other.

    I think for now, I won’t add this in but will leave this for a future day when I can find a better solution.

    Thanks
    E

  • OK. I see the problem. Unless WP were to update the term_relationships table so it could distinguish object_ids as belonging to a user or a post, the potential for conflict is unavoidable.

    My main goal is to run taxonomy queries for a user. It would work about as well for me to run meta queries for a user except that ACF stores the taxonomy terms in a serialized array in the user_meta table. I don’t know of a reliable way to run a meta query that looks for a value in a serialized array. If ACF stored taxonomy term ids each as a separate user_meta fields, then I could effectively run a user meta query. I realize that would be a big shift for ACF and I don’t expect you to take it on.

    I’ll likely solve this by creating my own user-taxonomy field. Since I know in my particular instance I won’t have term taxonomies shared between posts and users, I’ll probably store the taxonomy terms as such but I may explore storing them as separate meta fields instead. I very much appreciate that ACF is built with enough flexibility to make it straightforward for me to create my own field for this.

  • Hi @jordan314

    A good solution is to use a filter (acf/update_value) to run your own custom functionality!

    By using the normal taxonomy field, you can hook into the save process (filter above) and run your own logic: Use the $value to save the term relationships.

    You can find docs on this filter over on the docs page.

    Thanks
    E

  • Hi,

    I am using ACF 5 & the CPT-onomies plugin. I am having the same problem where the values in my ACF taxonomy field wont save if the taxonomy is a CPT-onomies taxonomy field…

    Any solutions?

  • Hello @elliot,

    I faced the same issue with taxonomies on the user editing screen. I believe ACF plugin should not allow creation of this type of field, if it’s not working anyway. Or at least show some notice about this kind of fields.

    Now it’s seems like a bug and I spent few hours to understand what is wrong.

  • Sorry for a late reply, but I’m trying to understand this…

    I would like to categorise my Users using a taxonomy. In fact, I’d like that taxonomy to be the same one as used by Posts.

    Does adding taxonomies to Users in ACF accomplish this, or not?
    Like the original poster, I can use ACF to successfully add, to my Edit User pages, both 1) my Posts categories taxonomy and 2) a custom taxonomy created by the plugin (new version, LH User Taxonomies).

    As for 2) the custom taxonomy – similar to the original post: checking the box for any of the options in either the ACF-derived checkbox list or the one put on the same page by the plugin produces a different result – ie. the checkmarks do not correspond, the data is going somewhere else.

    As for 1) adding the Posts category to a User edit page – how can I learn more about what exactly is happening when I save a user, having clicked on a Post category? Where is this data going?

    If ACF etc can’t be used, is there another way to accomplish this?

    Thanks.

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

The topic ‘Taxonomies not saving for users’ is closed to new replies.