I am looking for a SQL query to count all comments for a specific node type. What code should I use to achieve it?

  • 1
    Please share with us what you've tried. – Neograph734 Dec 22 '13 at 20:39

You can use this helper function

function mymodule_get_comment_count_by_node_type($node_type) {
  $query = new EntityFieldQuery();
  $result = $query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', $node_type, '=')
  $nids = array_keys($result['node']);

  $comments = db_select('comment', 'c')
      ->fields('c', array('cid'))
      ->condition('nid', $nids, 'IN')
  $comments_count = $comments->rowCount();
  return $comments_count;
| improve this answer | |

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.