0

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

Take a look at the arguments you're passing to get_comments() and compare them to the documentation on the WordPress Codex.

Your example -

$args = array(
    'include'      => $postids,
    'meta_query' => array(
      array(
        'key'   => 'viewed_status',
        'value' => 'unseen'
      )
    ),      
    'count' => true
);

The codex example -

$defaults = array(
    'author_email' => '',
    'ID' => '',
    'karma' => '',
    'number' => '',
    'offset' => '',
    'orderby' => '',
    'order' => 'DESC',
    'parent' => '',
    'post_id' => '',
    'post_author' => '',
    'post_name' => '',
    'post_parent' => '',
    'post_status' => '',
    'post_type' => '',
    'status' => '',
    'type' => '',
    'user_id' => '',
    'search' => '',
    'count' => false
);
  1. You're passing a multidimensional array when the docs clearly indicate a single dimensional array.
  2. You're also passing an array of 'Post IDs' when the docs clearly indicate that an integer is expected.
  • The docs aren't updated. As stated, using a loop to show the actual comments returns the correct comments so in theory it should return the correct count. Look at the docs for WP_Comment_query – Pollux Khafra Jan 15 '13 at 4:52
0

My solution is just to use count() to determine the number of items in the array returned by $comments which does the trick but I'm going to leave this unchecked as that doesn't explain why the function method isn't working.

0

I just tested your code as-is on my site (I manually added comment meta values to 3 comments) and it worked fine.

The only thing I did was comment out 'post_type' => 'fod_articles', since I don't have that post type. Is it supposed to be fod_articles or food_articles?

Edit:

I tested again, this time with a custom post type, and it worked after making this change:

$args = array(
    'post__in'      => $postids,
    'meta_query' => array(
      array(
        'key'   => 'viewed_status',
        'value' => 'unseen'
      )
    ),      
    'count' => true
);

Basically change include to post__in

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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