Using WordPress ‘delete_comment_meta()’ PHP function

The delete_comment_meta() WordPress PHP function removes metadata matching certain criteria from a comment. This function can match based on the key, or key and value, allowing for the removal of all metadata matching a key, if needed.

Usage

Here’s a simple example of using the function:

delete_comment_meta( 7, 'example_meta_key', 'example_value' );

In this case, it will remove the ‘example_value’ from the metadata ‘example_meta_key’ of the comment with ID 7.

Parameters

  • $comment_id (int, required): The ID of the comment.
  • $meta_key (string, required): The name of the metadata.
  • $meta_value (mixed, optional): The value of the metadata. If provided, only rows that match the value will be removed. Must be serializable if non-scalar. Default is empty (”).

More Information

See WordPress Developer Resources: delete_comment_meta()

This function was implemented in WordPress 2.9.0. There are no indications of it being depreciated as of the current WordPress version. Related functions include add_comment_meta(), get_comment_meta(), and update_comment_meta().

Examples

Removing All Metadata of a Key

This example removes all metadata with the key ‘example_meta_key’ for the comment with ID 5.

delete_comment_meta( 5, 'example_meta_key' );

Removing Specific Metadata

This example removes the metadata ‘example_meta_key’ only where the meta_value is ‘foo’ for the comment with ID 5.

delete_comment_meta( 5, 'example_meta_key', 'foo' );

Removing Metadata from Multiple Comments

This code removes ‘example_meta_key’ from comments 3, 4, and 5.

delete_comment_meta( 3, 'example_meta_key' );
delete_comment_meta( 4, 'example_meta_key' );
delete_comment_meta( 5, 'example_meta_key' );

Removing Multiple Metadatas from a Comment

This code removes ‘meta_key1’ and ‘meta_key2’ from the comment with ID 6.

delete_comment_meta( 6, 'meta_key1' );
delete_comment_meta( 6, 'meta_key2' );

Removing Metadata with a Non-Scalar Value

This example shows how to remove metadata with a non-scalar value (an array in this case) from the comment with ID 7.

$meta_value = array('key1' => 'value1', 'key2' => 'value2');
delete_comment_meta( 7, 'example_meta_key', $meta_value );