update_post_meta

Update post meta field based on post ID.

Description

(int|bool) update_post_meta( (int) $post_id, (string) $meta_key, (mixed) $meta_value, (string) $prev_value = '' ); 

Use the $prev_value parameter to differentiate between meta fields with the same key and post ID.

If the meta field for the post does not exist, it will be added.

Returns (int|bool)

Meta ID if the key didn't exist, true on successful update, false on failure.

Parameters (4)

0. $post_id (int)
The post id.
1. $meta_key (string)
The meta key.
2. $meta_value (mixed)
Metadata value. Must be serializable if non-scalar.
3. $prev_value — Optional. (string) => ''
Previous value to check before removing. Default empty.

Usage

  1. if ( !function_exists( 'update_post_meta' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // The post id. 
  5. $post_id = -1; 
  6.  
  7. // The meta key. 
  8. $meta_key = ''; 
  9.  
  10. // Metadata value. Must be serializable if non-scalar. 
  11. $meta_value = null; 
  12.  
  13. // Optional. Previous value to check before removing. 
  14. // Default empty. 
  15. $prev_value = ''; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = update_post_meta($post_id, $meta_key, $meta_value, $prev_value); 
  19.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post.php  
  1. function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) { 
  2. // Make sure meta is added to the post, not a revision. 
  3. if ( $the_post = wp_is_post_revision($post_id) ) 
  4. $post_id = $the_post; 
  5.  
  6. return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value);