47 Responses

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

    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.

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

    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?

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

    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!

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

    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’.

  5. Ivete
    Ivete October 21, 2011 at 6:45 pm | | Reply

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

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

    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.

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

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

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

    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 ;-)

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

    brilliant tutorial, Justin ! Thanks !

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

    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?

  11. Wordpress NewsWordpress News October 31, 2011 at 12:31 pm |
  12. Iv.Draganov
    Iv.Draganov November 11, 2011 at 10:46 am | | Reply

    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?

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

    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' );
    
    }
  14. Mike Schinkel
    Mike Schinkel December 6, 2011 at 1:54 pm | | Reply

    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

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

    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?

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

    Hi and thanks for all your articles.

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

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

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

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

    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!

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

      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>
      
  18. Jon
    Jon January 17, 2012 at 10:30 pm | | Reply

    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!

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

    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;
    }
  20. salocin
    salocin February 2, 2012 at 12:39 pm | | Reply

    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

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

    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);
  22. elvis
    elvis February 7, 2012 at 8:50 pm | | Reply

    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

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

    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

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

    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.

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

    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/

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

    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.

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

    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?

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.