最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - How to get the `comment_post_ID`?

programmeradmin5浏览0评论

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' => ,

                    )
                    wp_insert_comment($comment_array);
                }
           }

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.

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' => ,

                    )
                    wp_insert_comment($comment_array);
                }
           }

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.

Share Improve this question edited Jan 3, 2022 at 14:19 Rohit kc asked Jan 3, 2022 at 12:20 Rohit kcRohit kc 298 bronze badges 3
  • "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 Commented 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 Commented Jan 3, 2022 at 13:59
  • 1 I have updated the question. – Rohit kc Commented Jan 3, 2022 at 14:23
Add a comment  | 

2 Answers 2

Reset to default 0

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 maybe misunderstood, but you can create your comment thanks to https://developer.wordpress/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

发布评论

评论列表(0)

  1. 暂无评论