0

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!

2 Answers 2

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_map goes through each comment result and pulls out the user_id and creates a new array containing only the user IDs.
  • then we use array_unique to reduce the array to only unique values.
4
  • Thanks for the suggestion of using array_map. However, there was a "wordpress > error" on the front end when I applied it. I implemented this in a function via the codesnippets plugin. Maybe there's a better way to get an array of user_id? thanks in advance
    – manik
    Jun 29, 2021 at 3:01
  • Unexpectedly I managed to get the result in a way I didn't want before. I'm so sorry to answer my own questions, but I really appreciate your response @Paul !
    – manik
    Jun 29, 2021 at 4:19
  • When you need to build an array by selecting a specific property or index from an array of objects or associative arrays, array_column() (or WP's now unnecessary implementation, wp_list_pluck()) is a super nice shortcut for that array_map() :)
    – bosco
    Jun 29, 2021 at 16:57
  • @bosco ooh, cool, will give it a look... Never used it to be honest. Thanks for the heads up!
    – Paul G.
    Jun 29, 2021 at 18:18
0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.