I know there are several similar questions, but I couldn’t find the one I wanted.
I want to get all commenters’s on a post. But if there is a commenter who gives more than one comment, then he is not counted double. I don’t want to display them individually like:
foreach($comments as $comment){ //echo }
but rather get them in array form.
Here’s how I’ve tried. But this method still counts commenters who have more than one comment in double (as many as his own).
$args_user = array( 'role__in' => array( 'author', 'subscriber' ),
'fields' => 'ID'
);
$get_usersgroup = get_users($args_user);
$thepost_id = get_the_ID();
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( array(
'post_id' => $thepost_id,
'author__in' => $get_usersgroup,
'status' => 'approve',
) );
$totalcommenters = count ( $comments );
any help are really appreciate!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I’m a little unclear as to exactly the final data you’re looking for. You said “array form”, but that’s not specific enough.
So based on your code snippet, this code will provide you an array of all unique commenter user IDs.
The code is essentially the same as yours, but I’ve combined a few parts together as they don’t need to be their own separate PHP statement – they can be put straight into the Comment Query.
$cq = new WP_Comment_Query();
$comments = $cq->query(
array(
'post_id' => get_the_ID(),
'status' => 'approve',
'author__in' => get_users(
array(
'role__in' => [ 'author', 'subscriber' ],
'fields' => 'ID'
)
)
)
);
$all_comment_user_ids = array_map( function ( $comment ) {
return $comment[ 'user_id' ];
}, $comments );
$total_comments = count( $comments );
$unique_commenters = array_unique( $all_comment_user_ids );
$total_unique_commenters = count( $unique_commenters );
Your query itself was fine… you just had to post-process the results.
- the
array_mapgoes through each comment result and pulls out theuser_idand creates a new array containing only the user IDs. - then we use
array_uniqueto reduce the array to only unique values.
Method 2
I managed to get an array of user_ids of all commenters in a post with the following method:
$args_user = array( 'role__in' => array( 'author', 'subscriber' ),
'fields' => 'ID'
);
$get_usersgroup = get_users($args_user);
$thepost_id = get_the_ID();
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( array(
'post_id' => $thepost_id,
'author__in' => $get_usersgroup,
'status' => 'approve',
) );
$all_commenters_id = array(); //literally this small part is make big effect to the results
foreach($comments as $comment) :
$all_commenters_id[] = $comment->user_id;
endforeach;
$unique_commenters = array_unique( $all_commenters_id );
$total_unique_commenters = count ( $unique_commenters );
Hopefully useful for those of you who have a similar goal!
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0