Adds a meta field to the given post.
Description
Post meta data is called “Custom Fields” on the Administration Screen.
For historical reasons both the meta key and the meta value are expected to be “slashed” (slashes escaped) on input.
Parameters
$post_idintrequired- Post ID.
$meta_keystringrequired- Metadata name.
$meta_valuemixedrequired- Metadata value. Arrays and objects are stored as serialized data and will be returned as the same type when retrieved. Other data types will be stored as strings in the database:
- false is stored and retrieved as an empty string (
'') - true is stored and retrieved as
'1' - numbers (both integer and float) are stored and retrieved as strings Must be serializable if non-scalar.
- false is stored and retrieved as an empty string (
$uniquebooloptional- 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
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 );
}
View all references View on Trac View on GitHub
Related
| Uses | Description |
|---|---|
wp_is_post_revision()wp-includes/revision.php | Determines if the specified post is a revision. |
add_metadata()wp-includes/meta.php | Adds metadata for the specified object. |
| Used by | Description |
|---|---|
WP_REST_Font_Faces_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php | Creates a font face for the parent font family. |
wp_check_for_changed_dates()wp-includes/post.php | Checks for changed dates for published post objects and save the old date. |
WP_Privacy_Policy_Content::get_suggested_policy_text()wp-admin/includes/class-wp-privacy-policy-content.php | Checks for updated, added or removed privacy policy information from plugins. |
WP_Privacy_Policy_Content::_policy_page_updated()wp-admin/includes/class-wp-privacy-policy-content.php | Updates the cached policy info when the policy page is updated. |
WP_Customize_Manager::trash_changeset_post()wp-includes/class-wp-customize-manager.php | Trashes or deletes a changeset post. |
wp_add_trashed_suffix_to_post_name_for_post()wp-includes/post.php | Adds a trashed suffix for a given post. |
wp_generate_attachment_metadata()wp-admin/includes/image.php | Generates attachment meta data and create image sub-sizes for images. |
media_sideload_image()wp-admin/includes/media.php | Downloads an image from the specified URL, saves it as an attachment, and optionally attaches it to a post. |
wp_write_post()wp-admin/includes/post.php | Creates a new post from the “Write Post” form using |
add_meta()wp-admin/includes/post.php | Adds post meta data defined in the |
do_enclose()wp-includes/functions.php | Checks content for video and audio links to add as enclosures. |
_publish_post_hook()wp-includes/post.php | Hook to schedule pings and enclosures when a post is published. |
wp_check_for_changed_slugs()wp-includes/post.php | Checks for changed slugs for published post objects and save the old slug. |
wp_insert_post()wp-includes/post.php | Inserts or updates a post in the database. |
wp_trash_post()wp-includes/post.php | Moves a post or page to the Trash |
wp_trash_post_comments()wp-includes/post.php | Moves comments for a post to the Trash. |
wp_xmlrpc_server::add_enclosure_if_new()wp-includes/class-wp-xmlrpc-server.php | Adds an enclosure to a post if it’s new. |
wp_xmlrpc_server::set_custom_fields()wp-includes/class-wp-xmlrpc-server.php | Sets custom fields for post. |
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
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
_colorand the value ‘red’ but this custom field will not display in the post edit screen.In addition, if the
$meta_valueargument 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.
-
Just
Rolf Allard van Hagen 7 years ago
-
In some cases I prefer extra checks like checking if is empty and then delete it.
capbussat 4 years ago
Log in to add feedbackupdate_post_meta ( 7, 'fruit', 'banana' );alone does the same.delete_post_meta($post_id, $meta_key, $meta_value);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); }