Show count of all comments by meta_value from a users posts

Question

I’m trying to display the total number of comments for a users posts limited to a comment meta. I’ve tried using both get_comments() and WP_Comment_Query() but I get the same result of “1” for the comment count when it should be “3”. Now in this example I can remove 'Count' => true and just loop through the comments and that will return the correct amount of comments in a loop but the count still equals one.

They provide an example here on how to get the count but it doesn’t work for me. Here’s my function.

 function get_unseen_comment_count() {
    if ( !is_user_logged_in() )
        return false;
    $user_id = get_current_user_id();
    $postids = array();
    $posts = get_posts( array(
      'numberposts' => -1,
      'post_type'       => 'fod_articles',
      'author' => $user_id,
      'post_status'     => 'publish'
    ));

    foreach( $posts as $post ) {
        $postids[] = $post->ID;
    }
    $args = array(
        'include'      => $postids,
        'meta_query' => array(
          array(
            'key'   => 'viewed_status',
            'value' => 'unseen'
          )
        ),      
        'count' => true
    );
    // $comments_query = new WP_Comment_Query;
    // $comments = $comments_query->query( $args );
    $comments = get_comments($args);
    echo '<span class="comment-count">';
      echo $comments;
    echo '</span>';
 }

Now in the codex it doesn’t say that get_comment() supports meta query but you can see on line 181 here that it does. It doesn’t however show support for include but it definitely works when I test using a loop of the actual comments.

Some further testing when I remove the meta_query I do get the total comment count for all posts by the user so my media query seems to be the problem interfering with the count. I tried also just using meta_key and meta_value without a meta query but I get the same result…”1″.

0
Pollux Khafra 2 years 2020-01-07T08:41:06-05:00 0 Answers 76 views 0

Leave an answer

Browse
Browse