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.