Show count of all comments by meta_value from a users posts

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

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Show count of all comments by meta_value from a users posts

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.