How to add taxonomy to user?

Question

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?).

0
jost21 3 months 0 Answers 19 views 0

Leave an answer