update_metadata_by_mid

Update meta data by meta ID.

Description

update_metadata_by_mid( (string) $meta_type, (int) $meta_id, (string) $meta_value, (bool) $meta_key = false ); 

Parameters (4)

0. $meta_type (string)
Type of object metadata is for (e.g., comment, post, or user)
1. $meta_id (int)
ID for a specific meta row
2. $meta_value (string)
The meta value.
3. $meta_key — Optional. (bool) => false
Optional, you can provide a meta key to update it

Usage

  1. if ( !function_exists( 'update_metadata_by_mid' ) ) { 
  2. require_once ABSPATH . WPINC . '/meta.php'; 
  3.  
  4. // Type of object metadata is for (e.g., comment, post, or user) 
  5. $meta_type = ''; 
  6.  
  7. // ID for a specific meta row 
  8. $meta_id = -1; 
  9.  
  10. // The meta value. 
  11. $meta_value = ''; 
  12.  
  13. // Optional, you can provide a meta key to update it 
  14. $meta_key = false; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = update_metadata_by_mid($meta_type, $meta_id, $meta_value, $meta_key); 
  18.  

Defined (1)

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

/wp-includes/meta.php  
  1. function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) { 
  2. global $wpdb; 
  3.  
  4. // Make sure everything is valid. 
  5. if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { 
  6. return false; 
  7.  
  8. $meta_id = intval( $meta_id ); 
  9. if ( $meta_id <= 0 ) { 
  10. return false; 
  11.  
  12. $table = _get_meta_table( $meta_type ); 
  13. if ( ! $table ) { 
  14. return false; 
  15.  
  16. $column = sanitize_key($meta_type . '_id'); 
  17. $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; 
  18.  
  19. // Fetch the meta and go on if it's found. 
  20. if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { 
  21. $original_key = $meta->meta_key; 
  22. $object_id = $meta->{$column}; 
  23.  
  24. // If a new meta_key (last parameter) was specified, change the meta key,  
  25. // otherwise use the original key in the update statement. 
  26. if ( false === $meta_key ) { 
  27. $meta_key = $original_key; 
  28. } elseif ( ! is_string( $meta_key ) ) { 
  29. return false; 
  30.  
  31. // Sanitize the meta 
  32. $_meta_value = $meta_value; 
  33. $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type ); 
  34. $meta_value = maybe_serialize( $meta_value ); 
  35.  
  36. // Format the data query arguments. 
  37. $data = array( 
  38. 'meta_key' => $meta_key,  
  39. 'meta_value' => $meta_value 
  40. ); 
  41.  
  42. // Format the where query arguments. 
  43. $where = array(); 
  44. $where[$id_column] = $meta_id; 
  45.  
  46. /** This action is documented in wp-includes/meta.php */ 
  47. do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); 
  48.  
  49. if ( 'post' == $meta_type ) { 
  50. /** This action is documented in wp-includes/meta.php */ 
  51. do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); 
  52.  
  53. // Run the update query, all fields in $data are %s, $where is a %d. 
  54. $result = $wpdb->update( $table, $data, $where, '%s', '%d' ); 
  55. if ( ! $result ) 
  56. return false; 
  57.  
  58. // Clear the caches. 
  59. wp_cache_delete($object_id, $meta_type . '_meta'); 
  60.  
  61. /** This action is documented in wp-includes/meta.php */ 
  62. do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); 
  63.  
  64. if ( 'post' == $meta_type ) { 
  65. /** This action is documented in wp-includes/meta.php */ 
  66. do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); 
  67.  
  68. return true; 
  69.  
  70. // And if the meta was not found. 
  71. return false;