Everything You Should Know about WordPress Custom Fields
One of the most common needs for WordPress content is to add additional data to it beyond what already exists (category, author, content, etc.). If this data exists to organize posts, it’s “taxonomic” in nature and is best handled by custom taxonomies.
However, you may also want to add specific one-off, non-organizing data to your posts. This is where custom fields, or post meta, comes in.
What Custom Fields Are
WordPress custom fields are also called “post metadata” or
post_meta. So if you’re not careful, you can easily confuse yourself into thinking that a single thing is two different things. I’ll try to alternate names throughout this post to be clear that the two terms are interchangable.
Custom fields store additional metadata on a per-post level.
We use custom fields when we want to store additional metadata on a per-post level. WordPress posts automatically come with lots of metadata — their date of publication, their author, the time they were last modified — but you may want more.
For example, here on WPShout we frequently link to things elsewhere on the internet relevant to WordPress users. We’re doing that by storing the link to each post in a custom field. This lets us use our templates in more powerful and flexible ways than WordPress would have us out of the box.
Custom fields are simple key/value pairings that are stored and retrieved from the database with a few choice functions WordPress provides.
We’re simply storing this metadata in the database, using a set of functions to set and retrieve it that WordPress provides. Custom fields are unique from site options in that they exist at a post-level, but both systems are essentially simple key/value pairings that are stored and retrieved from the database with a few choice functions WordPress provides.
How Do I Add Custom Field Data to a Post?
Many plugins exist to make adding custom fields easy in WordPress. There’s also the default WordPress custom fields interface, which is, in general, rather clunky. The “Beyond the Basics” section below goes into a bit more detail about some of your options, but for now let’s just understand the default interface, because across sites it’ll always be there for you.
To use the default interface, find the meta box called “Custom Fields” on the post-editing screen. Select the name of your “key” from the dropdown, or “Enter new” if you’ve never used a given before; then you add your value, hit the button, and it’s added to your post, and saved with other data.
get_post_meta() and Other Functions for Retrieving Custom Field Data
get_post_meta()is the function I use most of the time.
Several PHP functions exist to allow you to use custom field data in your templates:
get_post_meta()is the function I use most of the time. It’s not got the greatest interface, but it works well. Its first (required) parameter is the post’s ID, the second (optional) is the name of the custom field you want the data for, and the final is a boolean flag for if you want the data as a string or array (
truefor a string).
get_post_custom— this function is a bit simpler to use than
get_post_meta, so using it can be nice for the sake of typing less. But because it has fewer options and powers, I tend to favor
get_post_meta(). The only, and optional, parameter for
get_post_custom()is the post’s ID. You’re getting the array of all values regardless, so inside the the loop
get_post_custom();is the equivalent of
Three other, less commonly used, functions exist for getting custom field data.
get_post_custom_values() will return all values for non-unique pairs.
get_post_custom_keys() will return an array contain the keys of all custom fields attached to a specific post ID, which can be a useful feature if you have a lot of values that may or may not be set on a specific post.
the_meta() — like other template tags that start with
echos (displays) the contents of all custom fields for the post; it’s not generally useful, because you have no control over the formatting you get, and it always shows all of a post’s custom field data.
How to Improve the Custom Fields Interface
One of the worst traits of WordPress custom fields out of the box is that the interface to edit them isn’t very powerful or intuitive.
One of the worst traits of WordPress custom fields out of the box is that the box to use them (which we explained above) isn’t very powerful or intuitive. In fact, it’s kind of the opposite of that. So that leaves a lot of opportunity for you to serve your clients and partners better by using some of the better interfaces that exist.
You can make better editing by using a Custom Fields plugin
There are a ton of plugins that make better interfaces for custom fields as well. The two that stick out in the my mind are Pods, and Advanced Custom Fields. In most cases this functionality travels on with the ability to create custom post types and taxonomies in a plugin, so the list I made in my tutorial about post types is pretty relevant if you’re looking to shop around.
My new favorite toolkit for post meta data and metaboxes
The route I actually recently converted to, and recommend for anyone looking for a bit more control and close-to-the code than a custom fields plugin, is to reach for the Custom Metaboxes and Fields for WordPress project from the fine folks at WebDevStudios. Compared to doing it from scratch, you’ll feel like you’ve got super powers!
What this set of files does for you is make it dead simple to make powerful and relatively fast to make great custom metaboxes which save automatically and benefit from most of the cool features of WordPress for things like images and media. You simply pull in their files and bootstrapper — the file that makes it all work like magic — into your plugin, use the prescribed hook with a big old configuration array and you’re set. It’s pretty magical the first time you try it.
While looking up the project, I see that the folks over at Human Made forked the project as well.
You may also try CMB2, another beautifully written open-source solution for creating WordPress custom fields.
On Creating your Own Post Meta Boxes
Because the interface is bad and there are better solutions, we recommend you use an existing system to add post metaboxes to your WordPress post edit screen. However, for someone looking to do the whole thing by hand, Justin Tadlock’s old tutorial on Smashing Magazine is still just about the state-of-the-art.
Additional Functions for Working With Post Meta Data
So far, we’ve only covered how to retrieve data from custom fields. Another common task you’ll have when coding, however, is changing or removing this data. For that, WordPress offers three simple functions:
add_post_meta()is how you set data the first time. You pass three parameters to it: the post ID; the name of the custom field; and the value you wish for the custom field to take. You can also pass an optional forth parameter, which dictates whether or not you want to require that this value be unique.
update_post_meta()can also be used to add the value, so it’s got the ability to stand in for
add_post_meta()if you’re not sure that the data is new. The first three parameters are the same as
add_post_meta, with the final optionl parameters being the previous value, which I’ve never used but could be useful if you’ve got many values set per key.
delete_post_meta()is a lot like you’d expect. The first three parameters are the same as the above functions, though in this case the final parameter (the custom field’s value) is optional; you’d only specify it if you’re only looking to remove one of many values set on your specified key.
The Power of WordPress Custom Fields
As we’ve said, custom fields are hugely important tool in elevating your use of WordPress past that of a basic article publishing engine. While it’s great to use all the built-in features of WordPress post types, adding a few more data fields is the secret to how, say, WooCommerce or any other similarly complex solution gives you easy access to and control over all the cool product features they make available.
Hopefully at the end you have a sense of what WordPress custom fields are — a basic key-value store for post-entities in WordPress — what they do — store your data simply — and how you can bend them to your will — that doesn’t lend itself to a five-word summary. Thanks for reading, and go make awesome stuff with WordPress!