My application allows users to add tags to an image (similar to facebook). Each tag can consist of up to 8 attributes and there is currently no limit on the amount of tags a user can add.

At the moment, tag information is stored across 3 post meta key/value pairs. Each post meta value is serialized before storing.

We're approaching a rewrite of this functionality and I wanted to ask for some advice on the best approach to storing tag data.

IMO there are two approaches:

  1. post meta key/value for each tag/attribute (this has the potential to create a huge dataset - n tags times 8 attributes)

  2. A new table just for tags

Both approaches would avoid storing serialized data, which I think is what I need to achieve.

Which is the better approach?
Is a third option possibly even more feasible?

2 Answers 2


Avoid serialized whenever you can. It is slow to read and you cannot really search for it.

Use custom tables – more than one:

  1. One for the relationship between users, tags and images: user_id, tag_id and image_id.
  2. One for tag meta data, your attributes, if I understand that right: tag_id, meta_1, meta_2

Now you can look up for each attachment ID if there is an entry for the user, and then look up the meta data from the meta table.

  • Thanks, I'll have a play around with a sample schema based on your suggestion
    – Oldie
    Apr 23, 2013 at 11:11

Rather than using a custom table or post meta to store the tag information you will probably find a simpler solution if you investigate the Custom Taxonomy support within WordPress.

You can register one (or more) custom taxonomies against the attachment post type.

The following Codex pages are a good place to start:

You may also find inspiration in existing WordPress plugins which provide similar functionality such as http://plugins.trac.wordpress.org/browser/matts-community-tags/trunk

  • I've had a play around with taxonomies and how I think it could work for tags with the application and I've got a couple of potential approaches. Feedback would be much appreciated. pastebin.com/79tnV1Ev
    – Oldie
    May 3, 2013 at 15:07
  • @Oldie your pastebin link doesn't work for me May 7, 2013 at 23:48
  • I've created a gist - gist.github.com/anonymous/dc2b058d7a36ae57c240
    – Oldie
    May 8, 2013 at 9:53
  • So looking at this the first approach is going to work more successfully because the second approach makes the data really hard to query using the WordPress APIs. May 8, 2013 at 15:47
  • Thanks Peter, I've created a POC based around using custom taxonomies. Initial implementation looked very promising. However, I have noticed a potential issue. For example, tag_t1 can include the term 'Accessories'. The user may wish to add more than 1 look tag with the term 'Accessories' under the tag_t1 taxonomy (Accessories > belt, Accessories > glasses). From what I can see, it doesn't seem to be possible to add the same term more than once for a taxonomy. Can you confirm? Thanks.
    – Oldie
    May 15, 2013 at 10:24

Your Answer

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

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