1

I'm in need of a taxonomy that can be shared between users and posts. Is this possible and if so how? I read that you can set up taxonomy for users, but is it possible to share it with a post type? My use case is a company employees list where employees are divided into different departments, corresponding to a post category for each department.

I want to be able to check which department current user belongs to, and show posts belonging to the same taxonomy term.

Or is there perhaps some other way of setting this up that might be easier or more natural?

1 Answer 1

2

No you can't, for a simple reason:

This term says that ID 1 is in the food category, but is that user 1 or post 1?

The ID provides no context, it's an object ID. As a result, there's no way to tell if that ID is a post ID or a user ID. This is why multiple post types can share a taxonomy as they're all posts, but you can't mix posts and users

Sidestepping the Issue

Use 2 taxonomies and a little magic! Create one for users, and one for posts, where every term exists twice in both with the same name/slug. Then use hooks and filters to create and update terms as they're modified in either taxonomy.

I anticipate that this might lead to the question of how to query for both at the same time. I'm afraid that's not possible. Instead, get_objects_in_term can get you the relevant users, and WP_Query can fetch the posts

8
  • Thanks, that clears up my initial confusion on this subject. Now on to how to actually set this up!
    – Johan Dahl
    Aug 23, 2017 at 14:48
  • Hmm I'm not qualified to answer really. But yes a taxonomy can be shared between different post types can it not? Should not Tom's comment be equally true in that case? Maybe my confusion isn't cleared up at all ^^
    – Johan Dahl
    Aug 23, 2017 at 14:57
  • 2
    Well, now I understand the answer. When you register a taxonomy for several post types, you are registering it actually for only 1 object type (post), so it is imposible that they share the same ID; but when you register the taxonomy for different object types, like user and post, they can share the same ID (they use separated database tables); functions like get_terms() that work with the object ID could not know if the given object is a user or a post in the case of shared ID.
    – cybmeta
    Aug 23, 2017 at 15:12
  • Ah. That explains it, thanks for clearing it up. I will have to make a connection between two separate taxonomies then it seems.
    – Johan Dahl
    Aug 23, 2017 at 15:15
  • Yes, a taxonomy can be shared by multiple post types, because they're all posts, and have the same table. At a data level, the taxonomy data doesn't mention the post type, just the ID of the object and the taxonomical data. Emphasis on object. As long as it's always the same kind of object then you're all good
    – Tom J Nowell
    Aug 23, 2017 at 15:17

Your Answer

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

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