delete_comment_meta example

$comments_to_delete = $wpdb->get_results( $wpdb->prepare( "SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp ), ARRAY_A );

    foreach ( (array) $comments_to_delete as $comment ) {
        $comment_id = (int) $comment['comment_id'];
        if ( ! $comment_id ) {
            continue;
        }

        $del_comment = get_comment( $comment_id );

        if ( ! $del_comment || 'trash' !== $del_comment->comment_approved ) {
            delete_comment_meta( $comment_id, '_wp_trash_meta_time' );
            delete_comment_meta( $comment_id, '_wp_trash_meta_status' );
        } else {
            wp_delete_comment( $del_comment );
        }
    }
}

/** * Retrieves metadata from a file. * * Searches for metadata in the first 8 KB of a file, such as a plugin or theme. * Each piece of metadata must be on its own line. Fields can not span multiple * lines, the value will get cut at the end of the first line. * * If the file data is not within that first 8 KB, then the author should correct * their plugin file and move the data headers to the top. * * @link https://codex.wordpress.org/File_Header * * @since 2.9.0 * * @param string $file Absolute path to the file. * @param array $default_headers List of headers, in the format `array( 'HeaderKey' => 'Header Name' )`. * @param string $context Optional. If specified adds filter hook {@see 'extra_$context_headers'}. * Default empty string. * @return string[] Array of file header values keyed by header name. */
$interval = 1;

        // akismet_as_submitted meta values are large, so expire them         // after $interval days regardless of the comment status         while ( $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT m.comment_id FROM {$wpdb->commentmeta} as m INNER JOIN {$wpdb->comments} as c USING(comment_id) WHERE m.meta_key = 'akismet_as_submitted' AND DATE_SUB(NOW(), INTERVAL %d DAY) > c.comment_date_gmt LIMIT 10000", $interval ) ) ) {
            if ( empty( $comment_ids ) )
                return;

            $wpdb->queries = array();

            foreach ( $comment_ids as $comment_id ) {
                delete_comment_meta( $comment_id, 'akismet_as_submitted' );
                do_action( 'akismet_batch_delete_count', __FUNCTION__ );
            }

            do_action( 'akismet_delete_commentmeta_batch', is_countable( $comment_ids ) ? count( $comment_ids ) : 0 );
        }

        if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11)$wpdb->commentmeta ) ) // lucky number             $wpdb->query("OPTIMIZE TABLE {$wpdb->commentmeta}");
    }

    // Clear out comments meta that no longer have corresponding comments in the database
'number'       => $number,
                'paged'        => $page,
                'order_by'     => 'comment_ID',
                'order'        => 'ASC',
            )
        );

        foreach ( (array) $comments as $comment ) {
            $comment_as_submitted = get_comment_meta( $comment->comment_ID, 'akismet_as_submitted', true );
            
            if ( $comment_as_submitted ) {
                delete_comment_meta( $comment->comment_ID, 'akismet_as_submitted' );
                $items_removed = true;
            }
        }

        // Tell core if we have more comments to work on still         $done = ( is_countable( $comments ) ? count( $comments ) : 0 ) < $number;
        
        return array(
            'items_removed' => $items_removed,
            'items_retained' => false, // always false in this example             'messages' => array(), // no messages in this example

    do_action( 'trash_comment', $comment->comment_ID, $comment );

    if ( wp_set_comment_status( $comment, 'trash' ) ) {
        delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' );
        delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' );
        add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved );
        add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() );

        /** * Fires immediately after a comment is sent to Trash. * * @since 2.9.0 * @since 4.9.0 Added the `$comment` parameter. * * @param string $comment_id The comment ID as a numeric string. * @param WP_Comment $comment The trashed comment. */
Home | Imprint | This part of the site doesn't use cookies.