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