One of the things that makes WordPress so powerful as a content management system is that the post_meta table starts as a nearly blank slate that you can easily add custom meta fields to.
How you use it can be determined by a use case-specific plugin, like WooCommerce, or using a custom fields plugin like Custom Fields Suite or Pods to create your own fields.
In some cases, you can mange WordPress meta data on your own using the variety of functions WordPress provides for adding post meta fields and populating their data. In this post I will be covering the basics of passing data into the post_meta table.
What I am showing in this article is limited by intention. To do anything more complicated, you would either need to use the
WPDB class or use a custom fields plugin that can do that kind of heavy lifting for you.
For many type of plugins or CMS sites that need just a few fields, you may be able to easily create and manage a few fields using a few simple functions.
Adding a Meta Field
You can add a meta field to a post using
add_post_meta(). This function allows you to add a field to a specific post. For example, to add a field called 'flavor' to a post, of any post type, with the ID of 12, with the value of 'vanilla' you would do this:
add_post_meta( 12, 'flavor', 'vanilla' );
In the last post, I covered using the custom field user interface in the post editor. It is important to note that fields whose keys start with an underscore are considered private, and will not be options in the custom post UI, while fields that do not start with an underscore will be accessible there.
This means that in our example above this field would be accessible via the custom field UI. If, on the other hand, you wanted it to be private, you would make the field name '_flavor'.
Another important thing to keep in mind is that, by default, the fields can have more than one value. The fourth argument of
add_post_meta() can be set to true to create a unique value field, which can only have one value.
This means that this:
add_post_meta( 12, 'flavor', 'vanilla' ); add_post_meta( 12, 'flavor', 'mint' ); print_r( get_post_meta( 12, 'flavor' );
Would return an array containing 'vanilla' and 'mint'. On the other hand, if we can set unique to true like this to prevent the second value from being added:
add_post_meta( 12, 'flavor', 'vanilla', true ); add_post_meta( 12, 'flavor', 'mint', true ); print_r( get_post_meta( 12, 'flavor' );
In this case, the return value would just be 'vanilla'.
Updating Post Meta Values
In the last example, I showed how to create a unique meta field, where only one value could be saved to the field. But what if you wanted to change the existing value? For this, we would use
update_post_meta(), like this:
add_post_meta( 12, 'flavor', 'vanilla', true ); update_post_meta( 12, 'flavor', 'mint' ); print_r( get_post_meta( 12, 'flavor' );
The result of this would be 'mint'.
Keep in mind that with both of these functions you can pass an array of data and it will be stored as one datum. WordPress automatically handles serializing the array into a string.
User and Comment Meta
So far we have discussed meta data for posts, but users and comments also have meta data. Both have equivalents to
update_post_meta() that work in identical ways, but with the tables specific to that content type.
For example, you can use
update_user_meta() exactly the same way that I described above for their post equivalents to add or update user meta. For comments you can use
In this series I have given you a basic introduction to adding and updating post meta data, using the custom field user interface or by using the functions that WordPress provides. Along with my series on working with meta data, you should now have an understanding of how to add and work with post meta data.
What you do with it is up to you, and that's a beautiful thing and the true power of WordPress as a content management system.
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post