add_meta

Add post meta data defined in $_POST superglobal for post with given ID.

Description

(int|bool) add_meta( (int) $post_ID ); 

Returns (int|bool)

Parameters (1)

0. $post_id (int)
The post id.

Usage

  1. if ( !function_exists( 'add_meta' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/post.php'; 
  3.  
  4. // The post id. 
  5. $post_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = add_meta($post_id); 
  9.  

Defined (1)

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

/wp-admin/includes/post.php  
  1. function add_meta( $post_ID ) { 
  2. $post_ID = (int) $post_ID; 
  3.  
  4. $metakeyselect = isset($_POST['metakeyselect']) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : ''; 
  5. $metakeyinput = isset($_POST['metakeyinput']) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : ''; 
  6. $metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : ''; 
  7. if ( is_string( $metavalue ) ) 
  8. $metavalue = trim( $metavalue ); 
  9.  
  10. if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) { 
  11. /** 
  12. * We have a key/value pair. If both the select and the input 
  13. * for the key have data, the input takes precedence. 
  14. */ 
  15. if ( '#NONE#' != $metakeyselect ) 
  16. $metakey = $metakeyselect; 
  17.  
  18. if ( $metakeyinput ) 
  19. $metakey = $metakeyinput; // default 
  20.  
  21. if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) ) 
  22. return false; 
  23.  
  24. $metakey = wp_slash( $metakey ); 
  25.  
  26. return add_post_meta( $post_ID, $metakey, $metavalue ); 
  27.  
  28. return false; 
  29. } // add_meta