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.
/** * 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.
isa_cleanup_comment_meta( 'my_comment_meta_key' );// @todo can change the meta key here
Questions and Comments are Welcome