How to check if `comment_meta` exists before inserting the comment?

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)),
                );
                   wp_insert_comment($comment_array);
            }
            }

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

Topic wp-insert-post comment-meta email plugin-development comments Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.