I want to get the total count of comments posted only for a specific content type including deleted to get the highest comment id no matter whether comment is been deleted. please help me or else suggest me other way to achieve this.


You can always dig around the database with a db query with something like:

$content_type = 'mycontenttype';
$query = db_select('comment', 'c');

$query->join('node', 'n', 'n.nid = c.nid'); //JOIN node with comment
$query->fields('c', array('cid')); //SELECT the fields from comment
$query->condition('n.type', $content_type, '=');
$query->orderBy('c.cid', 'DESC'); //ORDER BY comment id

$num_comments = $query->countQuery()->execute()->fetchField();
$latest_comment_cid = $query->execute()->fetchField();

EDIT In this example I assume you mean unpublished comments and not deleted. See Anil's answer if you want to track count of deleted comments as well.

|improve this answer|||||
  • Does this code give me the count of deleted comments too? – D-pak Aug 6 '14 at 7:27
  • @D-pak it gives you the count of published and unpublished comments. See my Edit and Anil's answer for more information about deleted comments. – Pontus Nilsson Aug 6 '14 at 7:28

You need to write a custom module to achieve this... I am afraid there won't be any contributed module which will give you stats even if comment is deleted... Please find steps below to solve your use case..

  • Create a custom module
  • Implement hook_schema in .install file and create a table to track comment inserts so that even if a comment is deleted you have an entry of comment
  • Implement hook_comment_insert in .module file to update above custom table you have created using hook_schema

function hook_comment_insert($comment) {
  // Reindex the node when comments are added.
  $node = node_load($comment->nid);
  if ($node->type == "YOUR_CONTENT_TYPE") {
     // Use db_insert to make an entry to track comments into your custom table 
|improve this answer|||||
  • Ok, I will do this, But will it give me that count as a field in views? – D-pak Aug 6 '14 at 7:29
  • @D-pak You will have to write views plugin for the above scenario like you will have to expose your custom table to the views. – Deepak Kumar Aug 8 '14 at 11:04

It is solved. this issue was there because I had replaced text-area with text-box on comment form. I just did revert it and it worked. But I don't think that just replacing fields this issue occurs.

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