I have extracted the reply emails. The original emails are saved as CPT and work as Tickets in my plugin. I want the reply email to go as a comment to the original email.

 for($j = 1; $j <= $total; $j++){
            $mail = $emails->get($j);
//This checks if the email is reply or not and if the email is reply make it comment to its original post. 
    if ((!empty($mail['header']->references )) && ($mail['header']->references ) == ($mail['header']->message_id )
                    $comment_array = array(
                        'comment_content' => $mail['body'],
                        'comment_post_ID' => ,


I have distinguished the reply email and original email but I don't how to keep those emails as comments to the original email. This is how my wp_insert_post looks like:

$post_array = array( 
            'post_content'  => $mail['body'],
            'post_title'    => $mail['header']->subject,
            'post_type'     => 'faqpress-tickets',
            'post_author'   =>  ucwords(strstr($mail['header']->fromaddress, '<',true)),
            'post_status'   => 'publish',
            'meta_input'    => array(   
                'User'      => ucwords(strstr($mail['header']->fromaddress, '<',true)),
                'From'      => preg_replace('~[<>]~', '', strstr($mail['header']->fromaddress, '<')),
                'Email'     => $mail['header']->Date,
                'ticket_id' => $mail['header']->message_id,   
      //wp_insert_post( $post_array );

If anyone knows how to do it. It will be a great help.

  • "The original emails are saved as CPT" - is the post ID available in the $mail['header'] (or somewhere in/via $mail)? Or when you created the CPT post, how did you reference the original email - is it via a post meta? If so, what's your code for saving the meta, or what is the meta key?
    – Sally CJ
    Jan 3, 2022 at 13:19
  • I am posting my wp_insert_post on answers please check it so you can get an idea.
    – Rohit kc
    Jan 3, 2022 at 13:59
  • 1
    I have updated the question.
    – Rohit kc
    Jan 3, 2022 at 14:23

2 Answers 2


Do you actually insert the comments right after you created the CPT post?

I.e. Both the snippets in the question are in the same scope, e.g. wp_insert_post( $post_array ); for($j = 1; $j <= $total; $j++){ ... wp_insert_comment($comment_array); ... }.

If yes, then you can store the post ID returned by wp_insert_post() to a variable and then just use that variable with the comment_post_ID in your comment's arguments. E.g.

$post_id = wp_insert_post( $post_array );

And then in the $comment_array, just use 'comment_post_ID' => $post_id.

But if the snippets run in different PHP session/execution, and if the post meta named ticket_id is unique for each post in your CPT, then you can use the WordPress meta query and get_posts() to get the post that references the original email.

So for example,

$posts = get_posts( array(
    'post_type'  => 'faqpress-tickets',
    'meta_key'   => 'ticket_id',
    'meta_value' => $mail['header']->message_id,
) );

if ( ! empty( $posts ) ) {
    $comment_array = array(
        'comment_content' => $mail['body'],
        'comment_post_ID' => $posts[0]->ID,

    wp_insert_comment( $comment_array );
  • I did as you said now all the comments or replies are now being added to the first post.
    – Rohit kc
    Jan 3, 2022 at 16:04
  • Does the revised answer help?
    – Sally CJ
    Jan 4, 2022 at 2:01
  • No, It still is the same. The comment_post_ID is not getting the ID it is supposed to have.
    – Rohit kc
    Jan 4, 2022 at 6:40
  • What's the full code you used? (show the entire code exactly as you put it in your file, including the part that inserts the comments) Can you post it on Pastebin.com?
    – Sally CJ
    Jan 4, 2022 at 9:12
  • I posted it on pastebin.com pastebin.com/KfjmiFYj
    – Rohit kc
    Jan 4, 2022 at 9:16

I maybe misunderstood, but you can create your comment thanks to https://developer.wordpress.org/reference/functions/wp_insert_comment/ which will return you the comment_id on success. In the parameters you can pass the post_id of the original mail in comment_post_ID

  • Yes, that's where I am getting the problem how do I get the post_id of the original mail. There can be dozens of original mails and their replies. So, the replies should automatically go as comments to the original post. That's the title of this question also suggests, how do I get the comment_post_ID ?
    – Rohit kc
    Jan 3, 2022 at 14:58

Your Answer

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

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