2

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.

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?

2 Answers 2

2

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 
}
1
  • 1
    Hi @kruti : "This answer was automatically flagged as low-quality because of its length and content", which I am sure you don't want. Rather than just post code, please try to provide a description/explanation of your code and include why you think it will solve the problem.
    – s_ha_dum
    Commented May 16, 2013 at 14:18
0

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.

1
  • FWIW you are really answering a whole other question here, that being: "How can I enable multiple-selections in a user taxonomy".
    – jerclarke
    Commented Feb 19, 2016 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.