meta_id, post_id, meta_key, meta_value

Meta for Custom Objects in WordPress

Custom Objects

WordPress has some default objects like posts, taxonomy, comments and users. Actually there are many more objects, but these principal ones deal with content directly.

A custom object is different from a custom post type. A custom post type is just a subtype of the post object. The difference is in just a property/attribute, i.e. post_type. Most of the times, we build custom functionality using custom post types.

At other times, we need custom objects. These are not posts, comments or users. That’s why you have to create separate tables and CRUD methods for them. This is, in fact, one of the primary reasons why a lot of developers prefer using custom post types over custom objects.

When to use them?

That decision is very simple in a way. Ask yourself if the object (content type) you are building needs to have post like displays and needs to use the WordPress template system. If yes, you should blindly use custom post types. All the extra code is not worth it.

Another question that should be asked, ideally, is if the attributes/properties of your object are same as (or at least similar to) the standard post. That is if it is a head-body/title-description kind of content. If yes, it’s a perfect match. If not, you always have the post meta for all the extra attributes.

Object Meta

I feel the extendability of WordPress in terms of content types is pretty much solely because of the object meta. Any attribute that your object has and can’t be fitted into the WordPress post/comment/user’s structure can be added to the meta. There are meta tables for users, comments and posts. (There’s no meta-system for taxonomy. A lot of plugins will add that for you, however.)

There are very simple crud methods for the meta. For example, there’s get_post_meta, update_post_meta and delete_post_meta. Similar functions for users and comments exist. Just replace post in the functions with user or comment. (Who uses add_XXXX_meta anymore?)

Universal Meta Functions

These are the functions behind all your meta-functions. The get_XXXX_meta is just a wrapper of get_metadata('XXXX',.... For example, see get_post_meta in wp-includes/post.php:

So, you could basically access the meta of any object using the get_metadata function and just use post, comment or user as the $object_type.

Meta Structure

So, obviously if the same functions manipulate meta for all three objects there has to be some similarity in how the meta is structured. Right?

meta_id, post_id, meta_key, meta_value

Right! The only difference I see is the umeta_id on the usermeta table. However, in any of these functions or even in the way meta work in WordPress, the meta_id is not important, the object_id is (post_id, comment_id, user_id), which is pretty much in place. Plus, look at the table names: wp_usermeta, wp_postmeta and wp_commentmeta.

Now let’s say I create a table called say wp_eventmeta, with the same table structure as other meta tables. That is, the table has four columns, meta_id, event_id ({$object}_id), meta_key and meta_value. In this case, will this function work?

Custom Meta for Custom Objects

It doesn’t work for a simple reason. The metadata functions use the _get_meta_table to validate the table name. If this function returns false, nothing else happens.

As you can see it just checks if the $wpdb object has the table name as a property. Obviously, our table won’t be there. To add our table, we’d have to add a simple hook to both the init and switch_blog (for multisite) actions:

And done! Now the metadata functions will work with our custom table. Also, you need not name the table eventmeta. You might want to use your custom prefix, say hrt:

Oh, but you’d still have to write your own CRUD methods for your custom objects. The meta, however, will be handled natively by WordPress this way. As a finishing touch, you might want to add wrappers just like WordPress does. For example:

What’s the use?

Plenty, if you think of it. I discovered this when looking at how WooCommerce stores the meta for order items which are custom objects!

Do let me know if you found this useful in any of your projects.

2 thoughts on “Meta for Custom Objects in WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *