Delete Specific comment meta From All Approved Comments

This function will delete WordPress comment meta for the specific comment meta key that you pass, from all approved comments. This will not delete the meta from comments that are still held in moderation.

PHP

/**
 * Clean up (delete) a specific comment_meta from all APPROVED comments.
 * 
 * @param string $comment_meta_key the comment meta key to delete
 */
function isa_cleanup_comment_meta($comment_meta_key) {
	
	$args = array(
	    'post_type' => 'any'
	);

	$comments = get_comments($args);
		 
	$arr = array();

	foreach ($comments as $comment) {

		$all_meta = get_comment_meta($comment->comment_ID);
		$status = wp_get_comment_status($comment->comment_ID);

		if (array_key_exists($comment_meta_key, $all_meta) && 'approved' == $status) { 
			delete_comment_meta($comment->comment_ID, $comment_meta_key);
		}
		
	}

	$count = count($arr);

	error_log("Deleted $comment_meta_key meta for $count comments");
}

Usage Example

In this example, the comment meta key that will be deleted is my_comment_meta_key. It is passed as the parameter when calling the function, as you see below. You can change my_comment_meta_key to your own meta key.

PHP

isa_cleanup_comment_meta( 'my_comment_meta_key' );// @todo can change the meta key here

See more:

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]