add_post_meta
Add meta data field to a post.
Description
(int|false) add_post_meta( (int) $post_id, (string) $meta_key, (mixed) $meta_value, (bool) $unique = false );
Post meta data is called Custom Fields on the Administration Screen.
Returns (int|false)
Meta ID on success, false on failure.
Parameters (4)
- 0. $post_id (int)
- The post id.
- 1. $meta_key (string)
- Metadata name.
- 2. $meta_value (mixed)
- Metadata value. Must be serializable if non-scalar.
- 3. $unique — Optional. (bool) =>
false
- Whether the same key should not be added. Default false.
Usage
if ( !function_exists( 'add_post_meta' ) ) { require_once ABSPATH . WPINC . '/post.php'; } // The post id. $post_id = -1; // Metadata name. $meta_key = ''; // Metadata value. Must be serializable if non-scalar. $meta_value = null; // Optional. Whether the same key should not be added. // Default false. $unique = false; // NOTICE! Understand what this does before running. $result = add_post_meta($post_id, $meta_key, $meta_value, $unique);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/post.php
- function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
- // Make sure meta is added to the post, not a revision.
- if ( $the_post = wp_is_post_revision($post_id) )
- $post_id = $the_post;
- return add_metadata('post', $post_id, $meta_key, $meta_value, $unique);
- }