Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
The only problems I can see with this solutions: 1) How to avoid that one adds more than one taxon to a post? 2) How to filter the custom taxonomy meta box to only show taxons that aren't already in use by some other of your custom post type entries/posts? 3) How does one get the visual connection between a taxon and a post? Btw: You also have the posts2posts plugin by scribu, which can make connections. –  kaiser Sep 19 '12 at 21:46
    
hi @kaiser. Thanks for your comment. 1) I think this is an advantage rather than a drawback. The canary could belong to Jack, Jill, Bob, Alice, everyone. There would just be a new post for each relationship, but it would be like a phantom post, a 'wrapper' that mostly just referred to the original. 2) I'm not sure what you mean here - is it the box on the post edit screen? I think this would only happen at the code level so UI is less important from my point of view. 3) I guess you would have to make a new admin page that showed the relationships? –  djb Sep 19 '12 at 21:55
add comment

2 Answers

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,

  • a) create a custom post type Birds
  • b) create a taxonomy Bird Type
  • c) create a meta box with two fields

    • 1) lists users in a drop down
    • 2) name of users bird

The process would be to,

  • a) add a Bird, such as Canary
  • b) add a taxonomy term that matches the name of the bird, e.g. Canary
  • c) add a user in my meta box associated with this bird
  • d) if available at the time of creation, add the given name the user chooses for the bird

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 using add_post_meta etc functions, you could write your function to use add_user_meta and update_user_meta instead. So on and so on.

This leaves clear separation between each of your elements.

  • Custom Post Type is for the Bird which might explain the bird, its origin, etc
  • Taxonomy is for the classification of the Bird
  • Meta Box / User Meta function is to associate Taxonomy and given name of Bird to user

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.

share|improve this answer
    
Hi @userabuser, thanks very much for this answer. I'm aware of the various taxonomy metadata options (including simple term meta) - the problem is, there's no consensus on how it's to be done (see the trac ticket I linked to), the various implementations variously create extra tables etc etc. I'm not sure I understand your suggestion with assigning the user to the post type? If I directly associated the user with the post (eg by keeping a postmeta field with various user ids in it) then I would have to have meta-metadata! –  djb Sep 19 '12 at 23:19
    
Taxonomy metadata is not standardized in core, hence the jury is out on that one for now. If you have a CPT named Birds, a Meta Box with a Post Meta field that queries your users, shown in a drop down for example. You can associate Jack to Canary via post meta field in the Meta Box attached to the post type. Its no more meta-meta than doing it via Taxonomies. Unless I misunderstand you, if your CPT is for the Bird Type, then you only need to associate the User and that can be done via post meta fields. I'd personally still add a Taxonomy for the CPT called Bird Types and also add Canary (cont) –  userabuser Sep 19 '12 at 23:30
    
... to that because I would prefer to associate a CPT with a Taxonomy so when querying I don't have to query the title for what would otherwise be a taxonomy. But that's just a personal preference. Hope that makes sense @djb. –  userabuser Sep 19 '12 at 23:34
    
Ah - these are problems I don't have! taxonomising the canary itself as a 'bird type' is by the by :-). The question is more, 'how can i implement a more subtle taxonomy system without relying on potentially broken term meta implementations' than 'what are some equivalent alternatives to the existing taxonomy system'. –  djb Sep 19 '12 at 23:45
    
@djb I've added an update in attempt to clarify my thoughts on the process. That's probably the most sound and sane approach to this I can think of at the moment. –  userabuser Sep 20 '12 at 0:04
show 1 more comment

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.

share|improve this answer
    
Hi @helenhousandi. Thanks for your answer. Posts 2 Posts, while a wonderful plugin, doesn't do what I'm interested in - I want to misuse posts as taxonomies. The reason I want to misuse them is that I think it might be more robust than relying on one of the term meta plugins - and it would be more flexible. This method doesn't preclude many-to-many relationships - to relate a post to many users you could make as many wrappers as you liked, giving them each an author to match their owners. To relate a user to many posts, you just give them more wrappers. –  djb Sep 20 '12 at 8:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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