How to add taxonomy to user?

时间:2020-11-01 作者:jost21

I added some custom taxonomy to a CPT with:

register_taxonomy( \'sport\', \'activity\', [
        \'label\'     => \'Sport\',
        \'rewrite\'   => [ \'slug\' => \'sport\'],
        \'hierarchical\' => true
    ]);

and this works fine. The taxonomy also shows up in the admin menu, as a subpage for activity.

Then I wanted to do the same for the users:

    register_taxonomy( \'team\', \'user\', [
        \'label\'     => \'Team\',
        \'rewrite\'   => [ \'slug\' => \'team\'],
        \'hierarchical\' => true
    ]);

However this did not work for me, the taxonomy did not show up in the admin menu.
After some research, I could make it appear with this code

function add_user_tax_menu() {
    $tax = get_taxonomy( \'team\' );
    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
    );
}
add_action(\'admin_menu\', \'add_user_tax_menu\');

But it still does not work properly. For example if click on the link in the count column, I end up in the posts sections, with no posts listed. I guess I have to somehow tell WP that the type is user and not post, but I cannot figure out how.

I also tried using \'edit-tags.php?taxonomy=\' . $tax->name . \'&post_type=user\' instead, but that didn\'t work either (maybe because user isn\'t a post type?).

Update

With the help of the answer posted by @saqib-ali and the tutorial mentioned in the comment, I got somewhat closer to to a solution. However, I still cannot see which users are assigned to a team. When I click on the link in the Count column (see screenshot) I end up back in the posts section of the admin panel.

enter image description here

The link points to

.../wp-admin/edit.php?team=team-a

My guess is, that it needs to be changed to

.../wp-admin/users.php?team=team-a

according to the mentioned tutorial, this might be done with the manage_circle_custom_column hook. But I could not find a hook like that in the WP doc (only manage_posts_custom_column). So maybe that hook doesn\'t exist (anymore)?

And then I would probably also need to add a filter hook that filters users by the taxonomy in the query parameter. Similar to how it works for the role

.../wp-admin/users.php?role=administrator 

I couldn\'t yet find the appropriate hook for either task.

1 个回复
SO网友:Saqib Ali

wordpress中有一个名为Parent File的钩子(过滤管理菜单子菜单项的父文件)了解更多信息here

在您的情况下,它是这样的:

add_filter(\'parent_file\', \'parent_menu\');

function parent_menu($parent = \'\') {
    global $pagenow;
    
    if(!empty($_GET[\'taxonomy\']) && $pagenow == \'edit-tags.php\' && $_GET[\'taxonomy\'] == \'category\') {
        $parent = \'edit.php\';
    }
     /*If we\'re editing one of the user taxonomies
    We must be within the users menu, so highlight that*/
    if(!empty($_GET[\'taxonomy\']) && $pagenow == \'edit-tags.php\' && $_GET[\'taxonomy\'] == \'team\') {
        $parent = \'users.php\';
    }
    
    return $parent;
}
希望它能解决你的问题。

相关推荐

Order users by user role

我在团队页面中显示的用户配置文件中有自定义字段。上面写着“主任”、“研究员”、“毕业生”、“实习生”等。添加新团队成员时,可以从带有选项的选择框中进行选择。现在,页面按创建日期顺序显示用户,但我需要按层次顺序显示他们(首先是所有董事,然后是研究人员,然后是毕业生,等等)。配置文件的新字段位于函数中。php,代码如下:<!-- ROLE --> <?php $role = get_user_meta($user->ID, \'member_role\', true)