65 Responses

  1. galen
    galen October 20, 2011 at 3:15 pm |

    It’s features like this that take WordPress beyond being just another piece of blogging software. I think we’ll start seeing even more non-blog sites powered by WordPress in the next few years.

    Also, thanks for another great tutorial, Justin.

    Reply
  2. Boone Gorges
    Boone Gorges October 20, 2011 at 3:20 pm |

    Thanks for this, Justin!

    I’ve never messed with this much, but I’ve always wondered if conflicts would arise with the fact that wp_term_relationships has a column for object_id, but no way of differentiating between different kinds of objects. That is, if you apply a term to a post with ID = 34, and another term to a user with ID = 34, does it cause problems? Presumably, for this reason, you couldn’t use the same taxonomy for users and for posts/CPTs?

    Reply
  3. Chris Aprea
    Chris Aprea October 20, 2011 at 5:39 pm |

    I really hope they add more core support for user taxonomies in future WordPress releases. It’s really unfortunate that you have to hack together a lot of the functions that make user taxonomies actually useful. Nonetheless, very insightful tutorial Justin, as usual. Cheers!

    Reply
  4. shawn
    shawn October 21, 2011 at 12:56 am |

    I’ve been looking forward to this post from the first time I read the tweet.

    Thank you for such a detailed setup and explanation of what works and the current pitfalls. It’s really awesome to see tutorials and snippets like this that are way out of the wp ‘norm’.

    Reply
  5. Discover Custom user taxonomies with Justin Tadlock | WereWP
  6. Ivete
    Ivete October 21, 2011 at 6:45 pm |

    This is incredibly timely for a project I’m working on right now, thank you for posting it!

    Reply
  7. Tacklr
    Tacklr October 24, 2011 at 10:02 am |

    Thanks for such detailed explanation, Justin. Superb as usual. I didn’t know that user objects are the same as posts and links. You already put an idea in my head (of implementing a project using these methods).

    In another note, when I enter the site with the www. subdomain it redirects to a Google 404 page. Maybe your host didn’t add the www subdomain automatically to your domain name.

    Reply
    1. Tacklr
      Tacklr October 24, 2011 at 10:16 am |

      Actually, there’s no redirection at all. It shows a blank Google 404 page with a link to Google homepage.

      Reply
  8. Pablo Nakamura
    Pablo Nakamura October 26, 2011 at 5:41 pm |

    Excellent, as always.

    Presumably we could also create a hierarchical custom taxonomy for users, such as geographical values, e.g. Continent > Country > Region etc ?

    If so then this opens up some really interesting opportunities ;-)

    Reply
  9. mat
    mat October 27, 2011 at 10:31 am |

    brilliant tutorial, Justin ! Thanks !

    Reply
  10. Awesome Tutorial On User Taxonomies
    Awesome Tutorial On User Taxonomies October 31, 2011 at 9:01 am |
  11. WPTavern: Awesome Tutorial On User Taxonomies | WordPress Planet
  12. Listweb
    Listweb October 31, 2011 at 10:42 am |

    I created one taxonomy in my post but when i update without check any they dont have a default taxonomy.

    How i can set a default taxonomy?

    Reply
  13. Wordpress NewsWordpress News
    Wordpress NewsWordpress News October 31, 2011 at 12:31 pm |
  14. Awesome Tutorial On User Taxonomies | Backlynx
  15. Iv.Draganov
    Iv.Draganov November 11, 2011 at 10:46 am |

    Great tutorial! And just in time for a project I am working on. I implemented this for a “Location” taxonomy that is used for grouping users by country. However I noticed that when a user is deleted the term relations are not deleted and the term count remains the same. Is there an easy fix for that? Something to run on the ‘delete_user’ action maybe?

    Reply
  16. Iv.Draganov
    Iv.Draganov November 14, 2011 at 11:02 am |

    In order to delete user object to term relations upon user deletion use this function on the ‘delete_user’ action hook:

    add_action( 'delete_user', 'devpress_delete_user_object_term_relationships' );
    
    functiondevpress_delete_user_object_term_relationships( $user_id ) {
    
    	wp_delete_object_term_relationships( $user_id, 'location' );
    
    }
    Reply
  17. Mike Schinkel
    Mike Schinkel December 6, 2011 at 1:54 pm |

    Hi Justin,

    Nice article, but I hope you’ll consider improving this theme so that source code is presented better than just simple large-size fixed-width font. I yearn for your prior theme…

    -Mike

    Reply
  18. Monkeyboy
    Monkeyboy December 6, 2011 at 6:13 pm |

    Great article. The benefits of taxonomies are clear, and I want to start using them on my site. So I’m kind of dabbling at the moment, behind the scenes with them, on my website.

    Quick question though, that kind of puzzles me about the creation of taxonomies. Suppose I change my mind about a particular taxonomy, and no longer want it. Is it just a case of deleting the relevant call to register_taxonomy in functions.php? What would happen to any terms that have been added under that particular taxonomy? Do they stay in the database?

    Reply
  19. cedric
    cedric December 20, 2011 at 12:21 pm |

    Hi and thanks for all your articles.

    On that one, I’m just wondering where to apply the filters and actions you created. Thanks !

    Reply
    1. cedric
      cedric December 20, 2011 at 12:42 pm |

      Please ignore my previous question. I just didn’t know these filters were core and also working for taxonomies.

      Reply
  20. Zack Katz
    Zack Katz January 13, 2012 at 1:32 pm |

    If you add the following to the admin_head, it will simulate being on the right page:

    <script type="text/javascript">
    	jQuery(document).ready(function($) {
    		$('#menu-posts').removeClass('wp-menu-open wp-has-current-submenu').addClass('wp-not-current-submenu');
    		$('#menu-users').addClass('wp-has-current-submenu wp-menu-open menu-top menu-top-first').removeClass('wp-not-current-submenu');
    		$('#menu-posts a.wp-has-submenu').removeClass('wp-has-current-submenu wp-menu-open menu-top');
    	});
    </script>

    Thank you for this great tutorial; it’s been very helpful!

    Reply
    1. Zack Katz
      Zack Katz January 13, 2012 at 1:33 pm |

      Here’s the code, properly formatted:

      <script type="text/javascript">
      	jQuery(document).ready(function($) {
      		$('#menu-posts').removeClass('wp-menu-open wp-has-current-submenu').addClass('wp-not-current-submenu');
      		$('#menu-users').addClass('wp-has-current-submenu wp-menu-open menu-top menu-top-first').removeClass('wp-not-current-submenu');
      		$('#menu-posts a.wp-has-submenu').removeClass('wp-has-current-submenu wp-menu-open menu-top');
      	});
      </script>
      
      Reply
      1. Nathaniel Schweinberg
        Nathaniel Schweinberg September 17, 2012 at 9:57 pm |

        I’m going to update your code a bit, Zack!

        function cur_correct_current_menu(){ 
        	global $pagenow;
        	$tax = '';
            if ( !empty($_GET['taxonomy'] ) ) {
                $tax = $_GET['taxonomy'];
            }
        
        	if (is_admin()) {
        		if ($pagenow=='edit-tags.php' && !empty( $tax ) ) { 
        			if( $tax == 'profession' ){ 
        ?>
        
        	jQuery(document).ready(function($) {
        		$('#menu-posts').removeClass('wp-menu-open wp-has-current-submenu').addClass('wp-not-current-submenu');
        		$('#menu-users').addClass('wp-has-current-submenu wp-menu-open menu-top menu-top-first').removeClass('wp-not-current-submenu');
        		$('#menu-posts a.wp-has-submenu').removeClass('wp-has-current-submenu wp-menu-open menu-top');
        	});
        
        <?php 
        			}
        		}
        	}
        
        }
        
        add_action('admin_head', 'cur_correct_current_menu');
      2. Nathaniel Schweinberg
        Nathaniel Schweinberg September 17, 2012 at 9:58 pm |

        Let’s try that again…

        function cur_correct_current_menu(){ 
        	global $pagenow;
        	$tax = '';
            if ( !empty($_GET['taxonomy'] ) ) {
                $tax = $_GET['taxonomy'];
            }
        
        	if (is_admin()) {
        		if ($pagenow=='edit-tags.php' && !empty( $tax ) ) { 
        			if( $tax == 'staff_position' ){ 
        ?>
        
        	jQuery(document).ready(function($) {
        		$('#menu-posts').removeClass('wp-menu-open wp-has-current-submenu').addClass('wp-not-current-submenu');
        		$('#menu-users').addClass('wp-has-current-submenu wp-menu-open menu-top menu-top-first').removeClass('wp-not-current-submenu');
        		$('#menu-posts a.wp-has-submenu').removeClass('wp-has-current-submenu wp-menu-open menu-top');
        	});
        
        <?php 
        			}
        		}
        	}
        
        }
        
        add_action('admin_head', 'cur_correct_current_menu');
      3. Jon Masterson
        Jon Masterson March 7, 2013 at 9:59 am |

        I added this because the styling on “Posts” was still a bit goofy for me…

        $('#menu-posts .wp-not-current-submenu').css({"font-size":"13px","font-weight":"bold","line-height":"18px","padding":"0"});
        $('#menu-posts a.wp-has-submenu').css({"font-size":"13px","font-weight":"bold","line-height":"18px","padding":"0"});

        Thanks!

  21. Jon
    Jon January 17, 2012 at 10:30 pm |

    This is fantastic. I love WordPress but user types and permissions are definitely an area where it’s lacking. With this custom taxonomy stuff I think I can get my WP site doing everything I want. Thanks!

    Reply
  22. James
    James January 19, 2012 at 11:12 am |

    I’m in the initial stages of planning a project and may end up using some of this rather than going with additional user roles or user meta as I was first thinking.

    The following will set the right menu parent for your profession taxonomy.

    add_filter( 'parent_file', 'fix_user_tax_page' );
    
    function fix_user_tax_page( $parent_file = '' ) {
    	global $pagenow;
    
    	if ( ! empty( $_GET[ 'taxonomy' ] ) && $_GET[ 'taxonomy' ] == 'profession' && $pagenow == 'edit-tags.php' ) {
    		$parent_file = 'users.php';
    	}
    
    	return $parent_file;
    }
    Reply
  23. salocin
    salocin February 2, 2012 at 12:39 pm |

    Thx Justin for a good tut.
    I am trying to use this and allow Admin to set Multiple Taxonomy term for each user.
    In your example, only 1 profession can be set for each.

    In my example, I Need to allow multiple Profession to be set.
    I have already changed my_edit_user_profession_section() to use checkbox instead of radio.

    2 problems
    - How to save all checkbox selected.
    - How to make each selected checkbox checked

    Stuck on

    <input type="checkbox" name="profession" id="profession-slug ); ?>" value="slug ); ?>" ID, 'profession', $term ) ); ?> />

    and

    * Sets the terms (we're just using a single term) for the user. */
      wp_set_object_terms( $user_id, array( $term ), 'profession', false);

    thx in advance

    Reply
  24. salocin
    salocin February 6, 2012 at 4:40 am |

    Found my own answer

    Change checkbox name to be profession[]

    then in saving options

    $term = $_POST['profession'] ;
    wp_set_object_terms( $user_id, $term, 'profession', false);
    Reply
  25. elvis
    elvis February 7, 2012 at 8:50 pm |

    Hi Justin,
    thanks for this, is a fantastic tutorial, exactly what I was looking for.
    One thing I’d love to have, is this taxonomy showing in the ‘All Users’ screen (and preferably to be able to filter by it), I have found code to add a column but can’t figure out what code to use to pull out the taxonomy term/s.
    I was trying with

    $terms = wp_get_object_terms( $id, 'profession')

    but not sure if i’m totally in the wrong ball park!
    Any ideas, or links to other places that could help?
    thanks
    elvis

    Reply
  26. elvis
    elvis February 8, 2012 at 7:14 am |

    Hi,
    I managed to get the terms appearing in the column with wp_get_object_terms (simple error in column name!) but any ideas or links for how to set up a pulldown filter (like ‘category’ in all posts table).

    thanks
    elvis

    Reply
  27. steve
    steve February 10, 2012 at 6:23 am |

    Hi there Ive created a custom taxonomy called ‘province’ when I try to add these provinces to the menu (under appearance) it shows in red and says invalid.

    Any ideas why this is occurring?

    The custom taxonomy appears in my custom post type and everything seems to be working except I cant add it to the menu cause when I do it comes up as invalid.

    Reply
  28. Damian Gostomski
    Damian Gostomski March 5, 2012 at 5:19 pm |

    Some great code snippets here, really helped me out a lot.

    On a project I’ve been working on, needed a fair few user taxonomies, so I abstracted out a lot of the backend logic into a plugin, so all you have to do, is register a taxonomy in the normal way, and the rest of taken care of for you.

    I’ll be expanding on the functionality in the coming weeks, but it will hopefully save some others the need for some of the code here

    http://wordpress.org/extend/plugins/user-taxonomies/

    Reply
    1. David
      David July 9, 2012 at 11:01 pm |

      Great tutorial and plugin to simplify the workflow. Other users in the thread have been able to do this but I am having trouble with allowing users to enter more than a single term per taxonomy. I’ve changed the form on the user profile in the back-end to a checkbox but it will only save one at at time.

      Any help appreciated.

      Reply
  29. Scott
    Scott March 11, 2012 at 5:29 pm |

    Great tutorial on creating user taxonomies … really well explained.

    One question I have and can’t seem to find the answer to is given a user has selected (in my particular case) a number of terms from the custom taxonomy, what code is needed to find all selected terms for that user?

    In the tutorial you show the users for a particular term but not the other way around.

    Any help would be greatly appreciated.

    Thanks.

    Reply
  30. BC
    BC May 15, 2012 at 7:29 am |

    Awesome! Works great.

    I’m wondering how I could incorporate this into the front end?
    I have some basic edit profile fields set up which are working but how would I be able to add taxonomies to a front end edit page?

    Reply
  31. Catherine
    Catherine June 9, 2012 at 10:04 am |

    THE MOST USEFUL WORDPRESS ARTICLE I’VE READ!

    Sure, I Gonna Share this !!!

    THANK YOU SO MUCH!

    Reply
  32. Anca
    Anca June 20, 2012 at 5:51 pm |

    Thanks, this is a great tutorial. I really appreciate this solid example using functionality that’s available but not obvious.

    I’m using this idea to create some access rules for users based on a category on posts (so the taxonomy terms are related to posts and users).

    Reply
  33. Nick Budden
    Nick Budden September 10, 2012 at 11:35 pm |

    Thanks for the great article! I think you forgot to include this line in the first block of code:

    add_action( ‘init’, ‘my_register_user_taxonomy’, 0 );

    Reply
    1. Mehdiway
      Mehdiway October 28, 2012 at 8:56 am |

      I almost went crazy, it didn’t wanna work ! Thank you so much !

      Reply
  34. » Get a User’s Custom Taxonomies
    » Get a User’s Custom Taxonomies September 12, 2012 at 12:05 am |
  35. MikePayne
    MikePayne December 7, 2012 at 1:19 am |

    Thanks Justin! Digging through your archives always lends a few treasures.

    Reply
  36. Pablo
    Pablo January 9, 2013 at 8:54 am |

    Great article! Due to a lack of experience, I’m not too comfortable manipulating code. So, I was wondering if there’s a plugin that’ll create custom user taxonomies? And, assuming there is, whether you’d advise to avoid a plugin for this type of task and stick to following this tutorial?

    Reply
  37. WordPress: Necessary Reading | Tom J Nowell
  38. Alvin
    Alvin March 22, 2013 at 4:18 pm |

    Is there a way to add tags to an user ? This way we can define skills and create a tag cloud of this ! Any suggestions ?

    Reply
  39. Alvin
    Alvin April 2, 2013 at 2:03 am |

    Hi Justin, thx for your reply

    I need my users to add their own tags/taxonomies , not predefined by me.
    Like when adding tags to a post , users can add multiple tags

    Sorry sort of a newbie on this , planning on creating a plugin ?

    Reply
  40. Rach
    Rach April 27, 2013 at 8:58 am |

    Hi,

    Thank you very much for this posting this. I found it very useful and I’m using it in a project I’m currently working on.

    I have a question:

    Since users and posts are stored in two different table, it is possible that we have a post with ID= 250 and a user with ID=250
    We are using the same function to set the terms wp_set_object_terms for both users and posts. Would it be a conflict between the two different objects? Would they override each others values?

    Thank you,
    Rach

    Reply

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.