Create Custom Post Meta in WordPress (Additional Post Data)
When WordPress saves a post, it stores post data like title, content, and excerpt in the same database table it stores the post in. It offers a way, as post meta, to store some additional information associated with a post. Learn how to store extra details with posts in WordPress using the default functions offered.

What is WordPress Post Meta?
When we want to store some additional information that is not stored in the posts table of the WordPress database, we use post meta boxes to save this information in the wp_postmeta table. This additional information is called post meta, and when it is saved in this separate table, it stores the post ID it is associated with, a key/identifier of post meta, and the value of post meta. This post meta can then be displayed anywhere on the website like on the homepage, archive page, single page, or even in a custom post table column in the admin dashboard.
Functions Used to Add WordPress Post Meta
Adding custom meta involves the use of a few default WordPress functions combined with a few hooks. These functions are used to register/add, retrieve, and update post meta whenever a post is created or updated. The following function is used to add a meta box to a post create/update form:
add_meta_box($id, $title, $callback, $screen = null, $context = 'advanced', $priority = 'default', $callback_args = null);
- $id: (string) (Required) - ID for the meta box id attribute.
- $title: (string) (Required) - Title of the meta box.
- $callback: (callable) (Required) - The function that will contain the actual HTML to render our meta box.
- $screen: (string|array) (Optional) - The screen or screens on which the meta box will be shown.
- $context: (string) (Optional) - Context of the screen where the meta box will be displayed. Possible values are "side", "normal", and "advanced". The default value is "advanced".
- $priority: (string) (Optional) - Priority of appearance of the meta box. Possible values ("default","low", "high"). The default value is "default".
- $callback_args: (array) (Optional) - Array of parameters passed as a second parameter to your callback function.
get_post_meta($id, $key, $single);
- $id: (int) (Required) - Unique ID of the post.
- $key: (string) (Optional) - Key of post meta in the post table.
- $single: (bool) (Optional) - Whether to return the value as a single value.
update_post_meta($post_id, $meta_key, $meta_value, $prev_check = '');
- $post_id: (int) (Required) - Unique ID of the post.
- $meta_key: (string) (Required) - Unique meta key for post meta.
- $meta_value: (string) (Required) - Value for post meta.
- $prev_check: (mixed) (Optional) - If provided, only post meta with the passed value will be updated.
How to Add WordPress Post Meta?
The implementation requires a few steps, we first add an action hook add_meta_box with a callback function. The callback function adds meta boxes on the post create/update screen and calls another callback to render the custom fields. Then we add another action hook save_post_{$post->post_type}, to save the values of these fields when a post is saved. We will put all this logic in a separate post-meta.php file and include this file in the functions.php file of the theme. We already have a custom post type "Property", and we will add two post meta fields for "Price" and "Area".
The following is the step-by-step explanation of the implementation:
- Add an action hook
add_meta_boxeswith anadd_property_metafunction. - Add the
add_meta_boxfunction in theadd_property_metafunction with the parameters "id", "title", "callback", and "screen" array. - Render the post meta fields in the callback function.
- Use the
get_post_metafunction to fetch saved post meta values. - Add an action hook "save_post_property" with a
save_property_metafunction. Thesave_post_propertyfunction is called when a post type "property" is saved. - Save the post meta using the
update_post_metafunction in thesave_property_metafunction. - Make sure to use
sanitize_metato sanitize the post meta value that is posted.
post-meta.php
<?php
// Add a meta box to our custom post type
add_action('add_meta_boxes', 'add_property_meta');
function add_property_meta(): void
{
add_meta_box('property_meta_box', 'Property Data', 'render_property_meta', ['property']);
}
// Function to draw our post meta
function render_property_meta($post): void
{
// Retrieve post metadata if its already saved
$property_price = get_post_meta($post->ID, 'property_price', true);
$property_area = get_post_meta($post->ID, 'property_area', true);
?>
<table class="form-table property-form-table">
<tbody>
<tr>
<th scope="row"><label for="property_price"><?php _e('Price', 'text-domain'); ?></label></th>
<td><input type="text" name="property_price" id="property_price" class="regular-text"
value="<?= $property_price; ?>"/></td>
</tr>
<tr>
<th scope="row"><label for="property_area"><?php _e('Area', 'text-domain'); ?></label></th>
<td><input type="text" name="property_area" id="property_area" class="regular-text"
value="<?= $property_area; ?>"/></td>
</tr>
</tbody>
</table>
<?php
}
add_action('save_post_property', 'save_property_meta');
function save_property_meta($post_id): void
{
// Prepare array of our post meta to loop through
$post_meta = ['property_price', 'property_area'];
foreach ($post_meta as $meta) {
if (isset($_POST[$meta])) {
// Save post metadata if it's submitted with the post
update_post_meta($post_id, $meta, sanitize_meta($_POST[$meta]));
}
}
}
After adding the above code to our post-meta.php, we need to include it in the theme's functions.php to render the post meta boxes on the property create/edit screen.
include('post-meta.php');The meta boxes that we added will show on the property form screen, as shown in the following screenshot:

Custom post meta is a WordPress feature that allows developers to extend post types by storing additional information with the post. We demonstrated how to add custom post meta using default functions like add_meta_box(), get_post_meta(), add_post_meta(), and update_post_meta() and easily manage custom data for posts. We stored post-specific data in our example, and it can be further modified to add repeater post meta that dynamically generates custom meta fields.