Sometimes the default custom post type fields might not be enough for your requirements
and you need to store additional data on a custom post type.
Fortunately WordPress supports something called post metadata which allows you to store
additional information about a post. Let's look at how you can use post metadata on a post or custom
post type. Metadata is a way to store additional information about a post in a WordPress database.
Post metadata is stored in the post meta table as key value pairs.
If you look at the structure of the post meta table in a WordPress database
you will see that it has four columns meta ID, post ID, meta key and meta value.
Post ID is the ID of the post that the metadata is associated with.
Meta key is the name of the field and meta value is its value.
You can add post meta to a post using the add post meta function. This function takes three
parameters the ID of the post, the name of the metadata and the value of the metadata.
So let's say you wanted to add a post metadata field to a post with the ID of 1
and you wanted to store say the location where the post was written as London. You would do this
using the following code calling add post meta passing in the post ID, the key and the value.
There are other functions for working with post metadata such as update post meta,
delete post meta and get post meta. You can read more about these and how to use them in
plugins in the metadata page in the plugin developer handbook.
Adding post meta via code is one way but it's also possible to enable site administrators
the ability to add post metadata via the WordPress admin interface.
One way to do this is via the custom fields panel for the post type.
To enable the custom fields panel you will first need to make sure your custom post type supports
metadata. To do this you need to add or update the supports argument to include support for custom fields.
Then when editing a custom post type click on the editor's options icon, preferences,
panels and enable the custom fields panel.
This will ask you to refresh the page and then you'll see a custom fields panel at the bottom of
the screen. Here you can add a new custom field and give it a name and a value.
For example you could store the ISBN of the book.
This panel uses the add post meta function under the hood to add the metadata to the post.
It is also possible to pre-populate the name field of the custom fields panel
with a list of predefined meta fields. To do this you need to hook into the post meta form
keys filter and add the names of the meta fields that you want to display in the custom fields panel.
The post meta form keys filter is fired just before the html of the custom fields panel is
rendered and passes in two parameters an array of meta keys and the post object. If no other keys
are predefined a query will be run to fetch the keys of any existing metadata for the post.
So by updating the array of meta keys in this filter you can add meta field keys to the custom
fields panel to make it easier for your users to add the correct metadata. Here is an example of
how you could do this. You register a callback function onto the post meta form keys filter
and set your accepted arguments to 2 to accept both arguments from the filter.
Then check if the post's type is book and if it is add the ISBN option to the keys array
and then return that array. If the post type is a book it will add the ISBN meta field to the custom
fields panel. Now if you create or edit a book with the custom fields panel enable
you'll see the ISBN field is available to add as metadata to the post.
Another way to allow site administrators to add post metadata is to use custom meta boxes.
Working with custom meta boxes however also requires a good understanding of developing
with security in mind but for now you can read about them in the custom meta boxes page
in the plugin developer handbook. You will learn how to work with meta boxes
in the plugin developer learning pathway.