Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

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.
share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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