delete_metadata_by_mid

Delete meta data by meta ID.

Description

delete_metadata_by_mid( (string) $meta_type, (int) $meta_id ); 

Parameters (2)

0. $meta_type (string)
Type of object metadata is for (e.g., comment, post, term, or user).
1. $meta_id (int)
ID for a specific meta row

Usage

  1. if ( !function_exists( 'delete_metadata_by_mid' ) ) { 
  2. require_once ABSPATH . WPINC . '/meta.php'; 
  3.  
  4. // Type of object metadata is for (e.g., comment, post, term, or user). 
  5. $meta_type = ''; 
  6.  
  7. // ID for a specific meta row 
  8. $meta_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = delete_metadata_by_mid($meta_type, $meta_id); 
  12.  

Defined (1)

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

/wp-includes/meta.php  
  1. function delete_metadata_by_mid( $meta_type, $meta_id ) { 
  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. // object and id columns 
  17. $column = sanitize_key($meta_type . '_id'); 
  18. $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; 
  19.  
  20. // Fetch the meta and go on if it's found. 
  21. if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { 
  22. $object_id = $meta->{$column}; 
  23.  
  24. /** This action is documented in wp-includes/meta.php */ 
  25. do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); 
  26.  
  27. // Old-style action. 
  28. if ( 'post' == $meta_type || 'comment' == $meta_type ) { 
  29. /** 
  30. * Fires immediately before deleting post or comment metadata of a specific type. 
  31. * The dynamic portion of the hook, `$meta_type`, refers to the meta 
  32. * object type (post or comment). 
  33. * @since 3.4.0 
  34. * @param int $meta_id ID of the metadata entry to delete. 
  35. */ 
  36. do_action( "delete_{$meta_type}meta", $meta_id ); 
  37.  
  38. // Run the query, will return true if deleted, false otherwise 
  39. $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) ); 
  40.  
  41. // Clear the caches. 
  42. wp_cache_delete($object_id, $meta_type . '_meta'); 
  43.  
  44. /** This action is documented in wp-includes/meta.php */ 
  45. do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); 
  46.  
  47. // Old-style action. 
  48. if ( 'post' == $meta_type || 'comment' == $meta_type ) { 
  49. /** 
  50. * Fires immediately after deleting post or comment metadata of a specific type. 
  51. * The dynamic portion of the hook, `$meta_type`, refers to the meta 
  52. * object type (post or comment). 
  53. * @since 3.4.0 
  54. * @param int $meta_ids Deleted metadata entry ID. 
  55. */ 
  56. do_action( "deleted_{$meta_type}meta", $meta_id ); 
  57.  
  58. return $result; 
  59.  
  60.  
  61. // Meta id was not found. 
  62. return false;