delete_metadata

Delete metadata for the specified object.

Description

delete_metadata( (string) $meta_type, (int) $object_id, (string) $meta_key, (string) $meta_value = '', (constant) $delete_all = false ); 

Parameters (5)

0. $meta_type (string)
Type of object metadata is for (e.g., comment, post, or user)
1. $object_id (int)
ID of the object metadata is for
2. $meta_key (string)
The meta key.
3. $meta_value — Optional. (string) => ''
Metadata value. Must be serializable if non-scalar. If specified, only delete metadata entries with this value. Otherwise, delete all entries with the specified meta_key. Pass null, false', or an empty string to skip this check. (For backward compatibility, it is not possible to pass an empty string to delete those entries with an empty string for a value.)
4. $delete_all — Optional. (constant) => false
Optional, default is false. If true, delete matching metadata entries for all objects, ignoring the specified object_id. Otherwise, only delete matching metadata entries for the specified object_id.

Usage

  1. if ( !function_exists( 'delete_metadata' ) ) { 
  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 of the object metadata is for 
  8. $object_id = -1; 
  9.  
  10. // The meta key. 
  11. $meta_key = ''; 
  12. $meta_value = ''; 
  13. $delete_all = false; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = delete_metadata($meta_type, $object_id, $meta_key, $meta_value, $delete_all); 
  17.  

Defined (1)

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

/wp-includes/meta.php  
  1. function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) { 
  2. global $wpdb; 
  3.  
  4. if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) { 
  5. return false; 
  6.  
  7. $object_id = absint( $object_id ); 
  8. if ( ! $object_id && ! $delete_all ) { 
  9. return false; 
  10.  
  11. $table = _get_meta_table( $meta_type ); 
  12. if ( ! $table ) { 
  13. return false; 
  14.  
  15. $type_column = sanitize_key($meta_type . '_id'); 
  16. $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; 
  17. // expected_slashed ($meta_key) 
  18. $meta_key = wp_unslash($meta_key); 
  19. $meta_value = wp_unslash($meta_value); 
  20.  
  21. /** 
  22. * Filters whether to delete metadata of a specific type. 
  23. * The dynamic portion of the hook, `$meta_type`, refers to the meta 
  24. * object type (comment, post, or user). Returning a non-null value 
  25. * will effectively short-circuit the function. 
  26. * @since 3.1.0 
  27. * @param null|bool $delete Whether to allow metadata deletion of the given type. 
  28. * @param int $object_id Object ID. 
  29. * @param string $meta_key Meta key. 
  30. * @param mixed $meta_value Meta value. Must be serializable if non-scalar. 
  31. * @param bool $delete_all Whether to delete the matching metadata entries 
  32. * for all objects, ignoring the specified $object_id. 
  33. * Default false. 
  34. */ 
  35. $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all ); 
  36. if ( null !== $check ) 
  37. return (bool) $check; 
  38.  
  39. $_meta_value = $meta_value; 
  40. $meta_value = maybe_serialize( $meta_value ); 
  41.  
  42. $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key ); 
  43.  
  44. if ( !$delete_all ) 
  45. $query .= $wpdb->prepare(" AND $type_column = %d", $object_id ); 
  46.  
  47. if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) 
  48. $query .= $wpdb->prepare(" AND meta_value = %s", $meta_value ); 
  49.  
  50. $meta_ids = $wpdb->get_col( $query ); 
  51. if ( !count( $meta_ids ) ) 
  52. return false; 
  53.  
  54. if ( $delete_all ) { 
  55. $value_clause = ''; 
  56. if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) { 
  57. $value_clause = $wpdb->prepare( " AND meta_value = %s", $meta_value ); 
  58.  
  59. $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s $value_clause", $meta_key ) ); 
  60.  
  61. /** 
  62. * Fires immediately before deleting metadata of a specific type. 
  63. * The dynamic portion of the hook, `$meta_type`, refers to the meta 
  64. * object type (comment, post, or user). 
  65. * @since 3.1.0 
  66. * @param array $meta_ids An array of metadata entry IDs to delete. 
  67. * @param int $object_id Object ID. 
  68. * @param string $meta_key Meta key. 
  69. * @param mixed $meta_value Meta value. 
  70. */ 
  71. do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); 
  72.  
  73. // Old-style action. 
  74. if ( 'post' == $meta_type ) { 
  75. /** 
  76. * Fires immediately before deleting metadata for a post. 
  77. * @since 2.9.0 
  78. * @param array $meta_ids An array of post metadata entry IDs to delete. 
  79. */ 
  80. do_action( 'delete_postmeta', $meta_ids ); 
  81.  
  82. $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ', ', $meta_ids ) . " )"; 
  83.  
  84. $count = $wpdb->query($query); 
  85.  
  86. if ( !$count ) 
  87. return false; 
  88.  
  89. if ( $delete_all ) { 
  90. foreach ( (array) $object_ids as $o_id ) { 
  91. wp_cache_delete($o_id, $meta_type . '_meta'); 
  92. } else { 
  93. wp_cache_delete($object_id, $meta_type . '_meta'); 
  94.  
  95. /** 
  96. * Fires immediately after deleting metadata of a specific type. 
  97. * The dynamic portion of the hook name, `$meta_type`, refers to the meta 
  98. * object type (comment, post, or user). 
  99. * @since 2.9.0 
  100. * @param array $meta_ids An array of deleted metadata entry IDs. 
  101. * @param int $object_id Object ID. 
  102. * @param string $meta_key Meta key. 
  103. * @param mixed $meta_value Meta value. 
  104. */ 
  105. do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); 
  106.  
  107. // Old-style action. 
  108. if ( 'post' == $meta_type ) { 
  109. /** 
  110. * Fires immediately after deleting metadata for a post. 
  111. * @since 2.9.0 
  112. * @param array $meta_ids An array of deleted post metadata entry IDs. 
  113. */ 
  114. do_action( 'deleted_postmeta', $meta_ids ); 
  115.  
  116. return true;