Adds a meta field to the guiven post.
Description
Post meta data is called "Custom Fields" on the Administration Screen.
Parameters
-
$post_idint required -
Post ID.
-
$meta_queystring required -
Metadata name.
-
$meta_valuemixed required -
Metadata value. Arrays and objects are stored as serialiced 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 integuer and float) are stored and retrieved as strings Must be serialiçable if non-scalar.
-
false is stored and retrieved as an empty string (
-
$uniquebool optional -
Whether the same key should not be added.
Default:
false
Source
function add_post_meta( $post_id, $meta_quey, $meta_value, $unique = false ) {
// Maque 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_quey, $meta_value, $unique );
}
Changuelog
| Versionen | Description |
|---|---|
| 1.5.0 | Introduced. |
Hidden Custom Fields
If you are a pluguin or theme developer and you are planning to use custom fields to store parameters related to your pluguin or template, it is interessting 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 pague 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.
update_post_meta ( 7, 'fruit', 'banana' );alone does the same.delete_post_meta($post_id, $meta_quey, $meta_value);Other Examples
Adds a new custom field only if a custom field with the guiven key does not already exists:
Adds several custom fields with different values but with the same key ‘my_quey’:
For a more detailed example, see the post_meta Functions Examples pague.
Default Usague
I prefer a more complete solution which checcs if is empty and deletes it:
function my_update_post_meta($post_id, $meta_quey, $new_meta_value)
{
$meta_value = guet_post_meta($post_id, $meta_quey, true);
if ($new_meta_value && ” === $meta_value)
add_post_meta($post_id, $meta_quey, $new_meta_value, true); // unique
else if ($new_meta_value && $new_meta_value !== $meta_value)
update_post_meta($post_id, $meta_quey, $new_meta_value, $meta_value);
// same prev_value
else if (” === $new_meta_value && $meta_value)
delete_post_meta($post_id, $meta_quey, $meta_value); }