Get current custom user taxonomy

I’m using the plugin User Groups which lets me group users with categories. The plugin is working essentially the same as using the manual method (custom user taxonomies) described by Justin Tadlock here.

On author.php I want to list the taxonomies the currently viewed user belongs to, and each list item should link to the taxonomy term page.

Read More

I use the following code to get current user ID and retrieve custom field values from the profile page:

<?php // Get current author

$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

?>

And I use the following code to list the custom user taxonomies:

<?php // List custom user taxonomies

wp_list_categories('taxonomy=user-group');

?>

“taxonomy=user-group” is fetching all of the terms created within the plugin.

Here’s a way to get current post taxonomies, but I’m looking to change it to custom user taxonomies.

Is there a way for me to change wp_list_categories() to only display the taxonomies I want? Should I be looking for another approach?

Related posts

2 comments

  1. Try this suggestion:

    $user_id = get_current_user_id();  // Get current user Id
    $user_groups = wp_get_object_terms($user_id, 'user-group', array('fields' => 'all_with_object_id'));  // Get user group detail
    foreach($user_groups as $user_gro)
    {
        echo $user_gro->name; // Get current user group name
        echo $user_gro->taxonomy; // get current user taxonomy 
    }
    
  2. Ok, problem solved. I ditched the plugin and got another one called User Taxonomies. It does all the boring part of creating custom taxonomies (saving fields etc.) You will do just as well by writing it all yourself, but i wanted to keep my code clean.

    After installing the plugin I registered my custom taxonomies manually like described on the plugin page, but added hierarchy => true to make the taxonomy act like categories instead of like tags. To allow multi selection on the profile-page i added this code to the plugin document user-taxonomies.php:

    <input type="checkbox" name="<?php echo $key?>[]" id="<?php echo "{$key}-{$term->slug}"?>" value="<?php echo $term->slug?>" <?php checked(true, is_object_in_term($user->ID, $key, $term))?> />
    

    Then I had to change the saving of fields with this function (added to the same file:

    /**
     * Save the custom user taxonomies when saving a users profile
     *
     * @param Integer $user_id  - The ID of the user to update
     */
    public function save_profile($user_id) {
        foreach(self::$taxonomies as $key => $taxonomy) {
            // Check the current user can edit this user and assign terms for this taxonomy
            if(!current_user_can('edit_user', $user_id) && current_user_can($taxonomy->cap->assign_terms)) return false;
            // Save the data
            $user_terms = ! is_array($_POST[$key]) ? array($_POST[$key]) : $_POST[$key];
            wp_set_object_terms($user_id, $user_terms, $key, false);
            clean_object_term_cache($user_id, $key);
        }
    }
    

    To show the taxonomy terms which the user belongs to, I used the following snippet:

    <?php the_terms( $curauth->ID, 'NAME-OF-TAXONOMY');?></p>
    

    Hope this helps someone. Credit goes to Timothy Wood and benjaminniess.

Comments are closed.