Taxonomies are very useful for all sorts of things. But as many have pointed out they suffer from their lack of ability to carry metadata.
Assuming I have only posts that don’t have ‘natural’ parents on my site, why shouldn’t I just taxonomise my posts by attaching them to a post_parent, and store my metadata in the parent’s post_meta?
FOR EXAMPLE
I have a CPT ‘birds’ and a taxonomy ‘user_bird_collection’.
I have a user, Jack, and I want to add the Canary to his collection.
With a taxonomy
I create (or append to) a term in the user_bird_collection called ‘Jack’ (or something more unique like his UID), adding the Canary to the term.
When Jack goes to mybirds.com/my_collection I show him all the birds in his term.
If he wants to go one better and name his canary, I’m in a pickle because I have to add metadata to the relationship between the canary and Jack’s collection.
With post_parents
I create a new post with Jack as the author, whose post_parent is the ID of the canary.
When Jack goes to the collection page I show him all the posts that belong to him, and I cleverly divert all the demands for bird details, images etc up to the original canary.
When he wants to name his canary, all I need to do is add metadata to Jack’s post – effectively his ‘own’ copy of the canary, a sort of wrapper around it.
OBVIOUS ADVANTAGES
- Querying is just as easy.
- I can add as much metadata as I like to the original bird OR the personalised bird using post_meta.
- I’m not tied to one of several competing implementations of term meta.
What’s the problem with doing it this way?
You can add taxonomy metadata, similar to
add_post_meta
and some examples of doing so include,Tutorial Walk-Through
Taxonomy Metadata Class (improved version of above)
I have personally used this class myself which was also authored from a reputable member and moderator here at WPSE
-> Bainternet
, it did what it said on the box.Plugin
Tutorial Walk-Through
That aside…
If you have a user, named Jack for instance, then why don’t you assign the user to the custom post type via a post meta field in a meta box?
So you’d be looking at,
add_meta_box
add_post_meta
You can still use custom taxonomies to help you further classify or group your data, but a lot of what you are trying to achieve is quite simply the same as if you were doing it with posta meta data to begin with (i.e. add_post_meta)
And to simplify things, you can you a meta box class to roll out your solutions,
http://en.bainternet.info/2012/how-i-add-a-wordpress-metabox
http://www.deluxeblogtips.com/meta-box/
http://www.farinspace.com/wpalchemy-metabox/
I have used all of the above, each of which are good. I still use WPAlchemy and the Meta-Box class from Deluxe Blog Tips. As for Bainternet’s, there’s nothing wrong with that class, its a matter of using too many already. So take your pick.
UPDATE
My approach to this would be to,
c) create a meta box with two fields
The process would be to,
Using a repeatable field that has an Add Now button, you can add another user. But instead of grouping all of the meta fields into one post
meta_key
, you need to create separate post meta keys for each user you wish to associate with the bird, otherwise you’ll end up with serialized data which is slow to query.Even this approach can be improved.
You could instead associate the Taxonomy “Canary” to the user via User Meta instead.
For each type of bird in the users collection, you would use add_user_meta to apply a name the user chooses for the bird.
Your meta box that you create via
add_meta_box
isn’t strictly limited to usingadd_post_meta
etc functions, you could write your function to useadd_user_meta
andupdate_user_meta
instead. So on and so on.This leaves clear separation between each of your elements.
The
post_parents
idea really isn’t a good one, I have to say, because creating a new Post for what is otherwise meta data in the first place equates to bloat when we already have the Taxonomy and User tables to go along with the Posts table for your custom post type.I don’t see much of an issue besides that there can only be one post parent. Using a posts-to-posts type of relationship, which allows for many-to-many, would be an alternative route, where your “taxonomy terms” are really posts of a custom post type and thus can have content (and even taxonomies, etc.) associated with them. The lovely Posts 2 Posts plugin makes this much easier to accomplish, and with UI to boot.
As of WordPress 4.4, Taxonomy term meta is now a part of WordPress core.
You can use the following new functions to manage term meta:
However, for the particular use case in this question, the most robust way to manage User relationship with a Post may be to use Posts2Posts (plugin created and maintained by scribu, a core contributor).
Using this plugin, you can create a relationship between a User and a Post and also add meta data to the relationship itself.