How would I count the number of times a comment meta field’s value is in a post’s entire comments?

How would I count (and display) the number of times a comment meta field’s value is a post’s entire comments?

e.g. meta key is “fish” and key value “shark” appears in 5 comments of a post.

1 Answer
1

You should be able to do a meta_query in a WP_Comment_Query():

$args = array(
    'post_id'    => 'post-id-here',
    'meta_query' => array(
        array(
            'key'     => 'fish',
            'value'   => 'shark',
            'compare' => 'LIKE'
        )
    )
 );

// Query the comments
$comment_query = new WP_Comment_Query( $args );

// Count the number of comments
$count = count ( $comment_query );

The WP_Comment_query() accepts a 'post_id' so you can search withing a specific post’s comments.

Leave a Comment