Support

Account

Home Forums ACF PRO User taxonomy – displaying filtered list in admin

Solving

User taxonomy – displaying filtered list in admin

  • I’m using ACF PRO to create user taxonomy fields. I have selected “Save Terms” and “Load Terms” in the field. I can select categories fine, and the count for number of users that have a given category shows in the category admin page (created with code below). However, if I click on the count, no users(posts) are displayed. What have I missed?

    I have been using various guides to help me setup categories, and have stripped out what I though ACF was doing for me.

    /**
     * Creat custom taxonomies.
     */
    add_action( 'init', 'mw_create_custom_tax' );
    
    function mw_create_custom_tax() {
    	register_taxonomy(
    		'interests',
    		array('user'),
    		array(
    			'label' => __( 'Interests' ),
    			'hierarchical' => true,
    			'public'       => true,
    		)
    	);
    }
    
    /* Adds the taxonomy page in the admin. */
    add_action( 'admin_menu', 'mw_add_interests_admin_page' );
    
    function mw_add_interests_admin_page() {
    
    	$tax = get_taxonomy( 'interests' );
    
    	add_users_page(
    		esc_attr( $tax->labels->menu_name ),
    		esc_attr( $tax->labels->menu_name ),
    		$tax->cap->manage_terms,
    		'edit-tags.php?taxonomy=' . $tax->name
    	);
    }
    
    function mw_set_interests_category_submenu_active( $submenu_file ) {
    	global $parent_file;
    	if( 'edit-tags.php?taxonomy=' . 'interests' == $submenu_file ) {
    		$parent_file = 'users.php';
    	}
    	return $submenu_file;
    }
  • WP does not natively support taxonomies for users. I’m not sure that terms are being saved for the user, but I could be wrong. I would check the DB that values are actually being saved.

    I can’t find any documentation online of making the count of terms on the admin page into a link to the user list page that only shows users with that term. The best thing I can find is this https://codebriefly.com/how-to-create-taxonomy-for-users-in-wordpress/

    I any case, for this to work you would need to use the pre_get_users hook to alter the user query, however WP_User_Query does not support a tax_query. So, even if ACF is saving the terms correctly this information will be useless on the user list page (/wp-admin/users.php). I would say that you could use a meta query here, but that will not work if you are using save/load terms because if this is set then the terms are not saved in post meta. The way I understand it ACF will only save values as object terms or as meta, but not both.

    What you need to find, which I cannot locate, is documentation on how to create an archive page based on a taxonomy that is assigned to users. This might give you an idea of how alter the user query in the admin, but as far as I can find this is not possible.

  • Thanks for your reply, John.

    Yes, I’m aware WP does not natively support taxonomies for users. I know it can be done, though, as LH User Taxonomies does it. Both that plugin and the ACF functions that save and load terms are based on Justin Tadlock’s article on user taxonomies as far as I’m aware.

    I guess I’ll need to study the code for LH User Taxonomies to try to find the answer. I’m not hugely experienced with PHP hence I was hoping ACF would do the work for me! Another option would be to use the plugin, but there’s load of stuff I don’t want in there, and besides, I really like the ACF category chooser field…

  • Does the plugin create a filterable column in the admin list? If it does then finding out how they do it is the way I would go.

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

You must be logged in to reply to this topic.