I am inserting comments via wp_insert_comment and while doing so I am adding email replies only. Now, I want to check if the comment is already present or not before inserting and if does exist do not perform wp_insert_comment. Here, is my code:

 // Here, we insert the replies as comment to their parent email. 
            if ((!empty($mail['header']->references )) && (preg_replace('~[<]~','',strstr($mail['header']->references, '@',true))) == (preg_replace('~[<]~','',strstr($mail['header']->references, '@',true))))     
                  //echo htmlentities($mail['header']->message_id);

                $posts = get_posts( array(
                    'post_type'  => 'post-type',
                    'meta_key'   => 'ticket_id',
                    'meta_value' => preg_replace('~[<]~','',strstr($mail['header']->references, '@',true)),
                ) );
            if ( ! empty( $posts )   ) {
                $comment_array = array(
                    'comment_content' => $mail['body'],
                    'comment_post_ID' => $posts[0]->ID,
                    'comment_author'  => ucwords(strstr($mail['header']->fromaddress, '<',true)),
                    'comment_author_email' => preg_replace('~[<>]~', '', strstr($mail['header']->fromaddress, '<')),
                    'comment_type'  => 'email_replies',
                    'comment_date'  => $mail['header']->Date,
                    'comment_meta'  => preg_replace('~[<]~','',strstr($mail['header']->message_id, '@',true)),

I am a still beginner so any help will be much appreciated.

1 Answer 1


First off, you need to fix the comment_meta value. It should be an array of meta key and value pairs, and not just the meta value.

So give the meta a key (or name), e.g. message_id, and then do something like so:

'comment_meta' => array(
    'message_id' => preg_replace('~[<]~','',strstr($mail['header']->message_id, '@',true)),

Now to check if a comment already exists by a specific meta, you can use get_comments() and the meta query, just like you did using get_posts().

So for example:

$comment_exists = (bool) get_comments( array(
    'type'       => 'email_replies',
    'meta_key'   => 'message_id',
    'meta_value' => preg_replace('~[<]~','',strstr($mail['header']->message_id, '@',true)),
    'count'      => true, // this means we're retrieving the number of comments only
) );

if ( ! $comment_exists ) {
    $posts = get_posts( ... );

    if ( ! empty( $posts ) ) {
        // insert the comment

And BTW, you should assign the above preg_replace()'s value to a variable and then use it with the comment_meta and meta_value above.

  • Ok I will do as you said. But what to do inside this: if ( ! $comment_exists ) { $posts = get_posts( ... ); this
    – Rohit kc
    Jan 5, 2022 at 14:13
  • Use the same code in the question.. i.e. if ((!empty($mail['header']->references )) && (preg_replace('~[<]~','',strstr($mail['header']->references, '@',true))) == (preg_replace('~[<]~','',strstr($mail['header']->references, '@',true)))) { put the example in my answer here. the rest is the same as the one in your post/question } Make sense?
    – Sally CJ
    Jan 5, 2022 at 14:29
  • Nothing happens. The problem here might be with references & message_id because reply emails have both references and message_id.
    – Rohit kc
    Jan 5, 2022 at 15:03
  • Let me see your code first - post it on Pastebin. But if you've used the correct meta key/value when inserting a comment and checking if there's an existing comment with the same meta, then the $comment_exists above would give you the correct results.
    – Sally CJ
    Jan 5, 2022 at 15:09
  • 1
    Ok, no need to worry. I just did your code from fresh and now it is working. I have also pasted my code here: pastebin.com/DddmzpU5 for you to check. I am really grateful for your support and I think I can learn a lot from you. If there is any way we could get connected it would be nice.
    – Rohit kc
    Jan 5, 2022 at 15:34

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.