add_post_meta( int $post_id, string $meta_key, mixed $meta_value, bool $unique = false ): int|false
Adds a meta field to the given post.
Contents
Description
Post meta data is called "Custom Fields" on the Administration Screen.
Parameters
-
$post_id
int Required -
Post ID.
-
$meta_key
string Required -
Metadata name.
-
$meta_value
mixed Required -
Metadata value. Must be serializable if non-scalar.
-
$unique
bool Optional -
Whether the same key should not be added.
Default:
false
Return
int|false Meta ID on success, false on failure.
More Information
Note that if the given key already exists among custom fields of the specified post, another custom field with the same key is added unless the $unique argument is set to true, in which case, no changes are made. If you want to update the value of an existing key, use the update_post_meta() function instead
Character Escaping
Because meta values are passed through the stripslashes() function, you need to be careful about content escaped with \ characters. You can read more about the behavior, and a workaround example, in the update_post_meta() documentation.
Source
File: wp-includes/post.php
.
View all references
function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
// Make sure meta is added to the post, not a revision.
$the_post = wp_is_post_revision( $post_id );
if ( $the_post ) {
$post_id = $the_post;
}
return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
}
Changelog
Version | Description |
---|---|
1.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Hidden Custom Fields
If you are a plugin or theme developer and you are planning to use custom fields to store parameters related to your plugin or template, it is interesting to note that WordPress will not show custom fields which have keys starting with an “_” (underscore) in the custom fields list on the post edit screen or when using the
the_meta()
template function. This can be for example used to show these custom fields in an unusual way by using theadd_meta_box()
function.The following example:
will add a unique custom field with the key name
_color
and the value ‘red’ but this custom field will not display in the post edit screen.In addition, if the
$meta_value
argument is an array, it will not be displayed on the page edit screen, even if you don’t prefix the key name with an underscore.Adding or Updating a Unique Custom Field
Adds a new custom field if the key does not already exist, or updates the value of the custom field with that key otherwise.
Top ↑
Feedback
Just
update_post_meta ( 7, 'fruit', 'banana' );
alone does the same. — By Rolf Allard van Hagen —In some cases I prefer extra checks like checking if is empty and then delete it.
delete_post_meta($post_id, $meta_key, $meta_value);
— By capbussat —Other Examples
Adds a new custom field only if a custom field with the given key does not already exists:
Adds several custom fields with different values but with the same key ‘my_key’:
For a more detailed example, see the post_meta Functions Examples page.
Default Usage
I prefer a more complete solution which checks if is empty and deletes it:
function my_update_post_meta($post_id, $meta_key, $new_meta_value)
{
$meta_value = get_post_meta($post_id, $meta_key, true);
if ($new_meta_value && ” === $meta_value)
add_post_meta($post_id, $meta_key, $new_meta_value, true); // unique
else if ($new_meta_value && $new_meta_value !== $meta_value)
update_post_meta($post_id, $meta_key, $new_meta_value, $meta_value);
// same prev_value
else if (” === $new_meta_value && $meta_value)
delete_post_meta($post_id, $meta_key, $meta_value); }