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.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityHow 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.
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.
post_id
to query the current post comments?
$args = array( 'post_id' => $post->ID, 'meta_query' => array( array( 'key' => 'joinus', 'value' => 'Yes', 'compare' => 'LIKE' ) ) ); // Query the comments $comment_query = new WP_Comment_Query( $args ); // Count the number of comments $count = count ( $comment_query ); // echo the number of comments echo $count;
$post->ID
is set? Where and how are you using this code?