Count comments made by certain user's role (count intersect array in php)
I've written code to display a table with columns containing:
- post date
- post title
- number of comments of users with certain groups (actually certain meta data).
The following code only works correctly until the fourth loop, which is the fourth row in the table.
function generate_post_report() {
if(is_page( 3815 )){
$current_user = wp_get_current_user();
$args = array(
'hide_empty' =0,
'orderby' = 'post_date',
'exclude' =array(1, 464), // desire id
);
ob_start();
$categories = get_categories($args);
if ($categories){
foreach ( $categories as $category ){
echo 'button onclick=openCity(event, \''.$category-name.'\')'.$category-name.'/button';
}
echo '/div';
foreach ( $categories as $cat ){
echo 'div id='.$cat-name.' class=tabcontent';
echo' h3'.$cat-name.'/h3';
// Query set here
$argsfive = array(
'cat' = $cat-term_id,
'order'= 'ASC',
'nopaging' = true
);
$commented_query = new WP_Query( $argsfive );
if ( $commented_query-have_posts() ) {
echo 'table width=100%';
while ( $commented_query-have_posts() ) {
$commented_query-the_post();
//inspect data each post
echo '
tr
td/td
tda href='. get_the_permalink() .' target=_blank rel=noopener'. get_the_date( 'd/m/Y' ) .'/a/td
td'. get_the_title() .'/td';
//HERE IS THE CODE I'M TALKING ABOUT
$cat_selected = $cat-slug;
$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',
) );
// Comment Loop
if ( $comments ) {
foreach ( $comments as $comment ) {
$entire_commenters[] = $comment-user_id;
}
}
$clean_entire_commenters = array_unique ( $entire_commenters ); //user may have commented more than once
$trimmed_commenters = array_intersect( $get_usersgroup, $clean_entire_commenters );
//CONFUSING CODE ENDs HERE
echo 'tdcenter'.count( $trimmed_commenters ).'/center/td
td/td
td/td
td/td
td/td
tr
';
}
echo '/table';
}
}
}
$output= ob_get_clean();
return $output;
} }
my questions is:
Is there any limitation on count() function in php
Is there an error on my site (running wordpress 5.4 nor php 7.2.13), or
Is there another better way to get the same result?