skip to Main Content

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


  1. the count ($result->count) is always zero

    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".

    Even in the var_dump($result), the public 'found_comments' => int 0 is also zero.

    That is because the no_found_rows arg defaults to true, which means SQL_CALC_FOUND_ROWS is disabled by default.

    But you should not enable it if you set count to true.

    How can I get the count of each authors comments count?

    Just initialize WP_Comment_Query without passing any query args and then use the query() method to get the total comments count, like so:

    $comment_query = new WP_Comment_Query;
    $count = $comment_query->query( $query );
    

    But if count is not set (to true), then you would want to set no_found_rows to true and number to a non-zero integer. Example:

    $query = array(
        // ... your args here.
        'count'         => false, // 1. Don't set or set to false
        'no_found_rows' => false, // 2. Set to false (default is true)
        'number'        => 5,     // 3. Set to a non-zero value
    );
    
    $comment_query = new WP_Comment_Query( $query );
    
    // total for the current results set, e.g. on page 1
    $count = count( $comment_query->comments );
    // total for all pages, same as when LIMIT is not set
    $total = $comment_query->found_comments;
    

    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 arg

    approved and moderated are not valid values for the status arg (which references the comment_approved column in the comments table), and the default statuses included in WordPress core are (see get_comment_count()):

    Name status value Description
    Pending hold or 0 Comment is awaiting moderation
    Approved approve or 1 Comment is approved
    Spam spam Comment is marked as spam
    Trash trash Comment is trashed
    Post Trashed post-trashed The comment’s post is trashed

    PS: approve and hold are not the actual database value, but WP_Comment_Query accepts those values in place of 1 and 0 respectively.

    Login or Signup to reply.
  2. I think in your function you can try use something like this:

    $result = new WP_Comment_Query($query);
    ....
    
    $views['counts'] = count($result->get_comments());
    

    The more details is here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search