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-database-phpmyadmin

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?

wordpress-custom-fields-metaboxMany 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:

  • the-matrixget_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 (true for 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 get_post_meta(get_the_ID());

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 the_, the_meta() 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

logo-pods-headerThere 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

Easy and quick to make something this good.

Easy and quick to make something this good.

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

clear-from-memory-keysSo 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!

Image credit: johnrobertshepherd, psd


13 Responses

Comments

  • Anna Smith says:

    I am just coming from Google blogger platform to WordPress. I never seen in my server inside. I just installed WordPress and just going keep updating my blog. But today I realize that I want to see inside the hosting.
    My question is How can I take a full of my site backup.?

  • Edward says:

    A potentially interesting subject, but I don’t feel much more enlightened about the custom fields box than I was before. Unless this article was written with plugin authors in mind, it would perhaps be more useful to a broader range of readers if some more useful examples of the value and use of custom fields were included. “cool to be able to link to things elsewhere on the internet […] we’re doing that by storing the link to each post in a custom field” – how does this differ from a slug, and what, practically, might I do with it?

    Thanks,

    E.

    • That’s a very fair point, Edward. I assumed a bit more familiarity with the topic than was probably wise. I find it really hard to know where all readers and and adjust accordingly, but I think you’re spot on that a more thorough introduction may have been helpful.

      The short summary of what custom fields are why you would use them is that sometimes you want to attach data to a post, or something else in WordPress, and that data doesn’t match very well to the default properties of post. Maybe I have posts that all refer to historical events, and I want to record the date of those events in a field other than the body of the post. I might try to change the “published” time of the post, but that would quickly cause massive confusion. That’s where you’d want to create a custom field for that “date_of_event” and be able to get it using the functions of custom fields. Does that shed any light?

  • Ninh says:

    Thanks David, this is very helpful article for getting started people like me

  • echris says:

    Is there any way to hide : Custom fields can be used to add extra metadata to a post that you can use in your theme. for a custom post type?

  • how to link custom field from some other plugin’s field backend (DB)?

  • E,g Suppose I add custom field from front end from to submit keyword and i want to go that keyword to yoast’s keyword field ?

Pingbacks