In my WordPress v5.8.1 I have multiple authors, I am building a new comments navigation view for each author.
There are tens of comments for each authors post, but below code I am not able to get the comments count for the respective authors posts:
function user_nav_counts($views) {
global $current_user, $wp_query;
unset($views['mine']);
unset($views['approved']);
unset($views['moderated']);
unset($views['spam']);
unset($views['trash']);
$author_ID = $current_user->ID;
$types = array(
array('status' => 'approved'),
array('status' => 'moderated'),
array('status' => 'trash')
);
foreach ($types as $type) {
$query = array(
'status' => $type['status'],
'type' => 'comment',
'post_author' => $author_ID,
'post_type' => array('song', 'book'),
'count' => true,
);
$result = new WP_Comment_Query($query);
if ($type['status'] == 'approved'):
$class = ($wp_query->query_vars['comment_approved'] == 'approved') ? ' class="current"' : '';
$views['approved'] = sprintf(__('<a href="/%s"' . $class . '>Approved <span class="count">(<span class="approved-count">%d</span>)</span></a>', 'approved'), ('wp-admin/edit-comments.php?comment_status=approved'), $result->count);
elseif ($type['status'] == 'moderated'):
$class = ($wp_query->query_vars['comment_moderated'] == 'moderated') ? ' class="current"' : '';
$views['moderated'] = sprintf(__('<a href="/%s"' . $class . '>Pending <span class="count">(<span class="moderated-count">%d</span>)</span></a>', 'moderated'), ('wp-admin/edit-comments.php?comment_status=moderated'), $result->count);
elseif ($type['status'] == 'trash'):
$class = ($wp_query->query_vars['comment_trash'] == 'trash') ? ' class="current"' : '';
$views['trash'] = sprintf(__('<a href="/%s"' . $class . '>Trash <span class="count">(<span class="trash-count">%d</span>)</span></a>', 'trash'), ('wp-admin/edit-comments.php?comment_status=trash'), $result->count);
endif;
}
return $views;
}
if (!current_user_can('edit_others_posts')) {
add_filter('views_edit-comments', 'user_nav_counts', 10, 1);
}
With the above code, I am able to build the new nav but the count ($result->count
) is always zero.
Even in the var_dump($result)
, the public 'found_comments' => int 0
is also zero.
How can I get the count of each authors comments count?
2
Answers
WP_Comment_Query
doesn’t have a property named$count
, and the above call would actually cause PHP to throw a notice saying "Undefined property: WP_Comment_Query::$count".That is because the
no_found_rows
arg defaults to true, which meansSQL_CALC_FOUND_ROWS
is disabled by default.But you should not enable it if you set
count
to true.Just initialize
WP_Comment_Query
without passing any query args and then use thequery()
method to get the total comments count, like so:But if
count
is not set (to true), then you would want to setno_found_rows
to true andnumber
to a non-zero integer. Example:So you would want to use the above if you want to paginate the comments, but if you simply want the total comments, then use the
count
arg instead.Edit: However, you could also simply use
get_comments()
like so:$views['approved'] = get_comments( $query );
if you don’t need to access anything else in the class instance.Note about the
status
argapproved
andmoderated
are not valid values for thestatus
arg (which references thecomment_approved
column in the comments table), and the default statuses included in WordPress core are (seeget_comment_count()
):status
valuehold
or0
approve
or1
spam
trash
post-trashed
PS:
approve
andhold
are not the actual database value, butWP_Comment_Query
accepts those values in place of1
and0
respectively.I think in your function you can try use something like this:
The more details is here