WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know how to count comment per post, user and in total but i am
having problems getting the total amount of comments post in a specific post type.

I guess i can loop trough all the posts and count each one
comments but i am looking for something faster and less
resource expensive

Appreciate your help

I got this so far (i think this is a bad way but i might be wrong):

function countCptCommentsTotal() {

    // FAQ ARGS
    $faqStatsArgs = array(
        'post_type'         =>  'some_post_type',
        'post_status'       =>  'publish',
        'posts_per_page'    =>  -1
    $faqstats_query = new WP_Query($faqStatsArgs);

    // THE LOOP
    $allAnswers = 0;
    while ($faqstats_query->have_posts()) {

        $curfaqid       =   get_the_ID();
        $allAnswers =   $allAnswers + countPostComments($curfaqid);


    return $allAnswers;
share|improve this question
Then please show us what you got. – kaiser Feb 12 '14 at 13:23
hey @kaiser - added the method i mentioned in my question. – Sagive SEO Feb 12 '14 at 13:28
up vote 1 down vote accepted

How about direct sql with a simple subquery to first get all the post ids of your custom post type ex:

function get_all_comments_of_post_type($post_type){
  global $wpdb;
  $cc = $wpdb->get_var("SELECT COUNT(comment_ID)
    FROM $wpdb->comments
    WHERE comment_post_ID in (
      SELECT ID 
      FROM $wpdb->posts 
      WHERE post_type = '$post_type' 
      AND post_status = 'publish')
    AND comment_approved = '1'
  return $cc;


$total_comments =  get_all_comments_of_post_type('some_post_type');
share|improve this answer
This looks gr8 @bainternet ;) - testing... – Sagive SEO Feb 12 '14 at 13:36
i must polish my sql query abilities - could have saved some time. as always - thanks for your help ;) – Sagive SEO Feb 12 '14 at 13:45

Simply do a plain MySQL Query that sums the comment_count row. The following plugin as an example:

namespace WPSE;

 * Plugin Name: (#134338) Get total Comment Count
 * Plugin URl:  http://wordpress.stackexchange.com/q/134338

defined( 'ABSPATH' ) or exit;

function commentCountTotal( $postType = 'post' )
    global $wpdb;
    return $wpdb->query( $wpdb->prepare(
        "SELECT sum( comment_count )
        FROM (
            SELECT comment_count
            FROM {$wpdb->posts}
            WHERE post_type = '%s'
            AND post_status = 'publish'
            AND comment_count > 0
            LIMIT 0 , 999
        ) as count",
    ) );

You can then just call it to display the total amount of this post type.

echo \WSPE\commentCountTotal( 'your-post-type' );
share|improve this answer
Thanks a lot @kaiser – Sagive SEO Feb 12 '14 at 14:17

Your Answer


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.