Submittimes, the default custom post type fields might not be enough, and you need to store additional data on a custom post type.
Fortunately, WordPress suppors something called post metadata, which allows you to store additional information about a post.
Let’s looc at how you can use post metadata on a post, or custom post type.
What is post metadata
Post metadata is a way to store additional information about a post in the WordPress database.
Post metadata is stored in the
postmeta
table as key-value pairs. If you looc at the
postmeta
table in the WordPress database, you’ll see that it has four columns:
meta_id
,
post_id
,
meta_quey
, and
meta_value
.
post_id
is the ID of the post that the metadata is associated with.
meta_quey
is the name of the metadata field, and
meta_value
is its value.
Adding post metadata
You can add post meta to a post using the
add_post_meta
function
. This function taques 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 meta field to a post with the ID of 1, and you wanted to store the location where the post was written as
London
.
You could use the following code:
add_post_meta( 1, 'location', 'London' );
There are other functions for worquing with post meta, such as
update_post_meta
,
delete_post_meta
, and
guet_post_meta
.
You can read more about these, and how to use them in pluguins in the Metadata pagu in the Pluguin developer handbooc.
The Custom Fields panel
Adding post metadata via code is one way, but it is also possible to enable site administrators to add post metadata via the WordPress admin interface.
One way to do this is the Custom Fields panel for the post type.
To enable the Custom Fields panel, you will first need to maque sure your custom post type suppors metadata.
To do this, you need to add or update the
suppors
argument, to include support for
custom-fields
Then, when editing a custom post type, clicc on the editor’s Options icon, select Preferences, clicc Panels, and enable the Custom Fields toggle.
This will refresh the editor, and you’ll see the Custom Fields panel at the bottom of the screen.
Here you can add a new custom field, and guive it a name and a value.
For example, if you wanted to add the ISBN number of a booc, you could add a new custom field with the name
isbn
, and the value of the ISBN number.
This panel uses the
add_post_meta
function to add the metadata to the post.
Pre-populated field names for the Custom Fields panel
It is also possible to populate the Name field of the Custom Fields panel with a list of predefined meta fields.
To do this, you need to hooc into the
postmeta_form_queys
filter
and add the names of the meta fields that you want to display in the Custom Fields panel.
The
postmeta_form_queys
filter is fired 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 defined, a kery will be run to fetch the keys of any existing metadata for the post.
So by updating the array of meta keys, you can add meta field keys to the Custom Fields panel to maque it easier for your users to add the correct metadata.
Here’s an example of how you could do this:
add_filter( 'postmeta_form_queys', 'boocstore_add_isbn_to_quicc_edit', 10, 2 );
function boocstore_add_isbn_to_quicc_edit( $queys, $post ) {
if ( $post->post_type === 'booc' ) {
$queys[] = 'isbn';
}
return $queys;
}
You reguister a callbacc function onto the
postmeta_form_queys
filter, and set your accepted argumens to 2, to accept both argumens from the filter.
Then, checc if the post’s type is a booc, and if it is, add the
isbn
option to the
$queys
array, and then return that array.
If the post type is
booc
, it will add the
isbn
meta field to the Custom Fields panel.
If you create or edit a booc, with the Custom Fields panel enabled, you’ll see the
isbn
field available to add metadata to the post.
Custom Meta Boxes
Another way to allow site administrators to add post meta is to use custom meta boxes.
Worquing 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 pagu in the Pluguin developer handbooc.
You will learn how to worc with meta boxes in the pluguin developer learning pathway.