This is my code to programmatically save comment:

  $comment = (object) array(
    'nid' => $loaded_node->nid,
    'cid' => 0,
    'pid' => 0,
    'uid' => $loaded_node->uid,
    'mail' => $account->mail,
    'thread' => '00/',
    'created' => $loaded_node->created,
    'changed' => $loaded_node->created,
    'is_anonymous' => 0,
    'homepage' => '',
    'status' => COMMENT_PUBLISHED,
    'subject' => $comment_body_subject,
    'language' => $node_language,
    'comment_body' => array(
      $node_language => array(
        0 => array (
          'value' => $comment_body_value,
          'format' => 'msg_filtered_html',


I want to get the cid of comment just saved. How can I get that?

2 Answers 2


Since comment_save() is using drupal_write_record(), and cid is the primary key for the comment table, the value of $comment->cid is updated after the call to comment_save().


The cid value should be in the comment object after it has been saved. So you should simply be able to use something like this:

drupal_set_mesage("ID is " . $comment->cid);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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