I have extracted a bunch of emails using PHP IMAP connection and assigned those emails to a custom post type. Here's the code how I assigned those emails to custom post type.
$total = $emails->total_msg();
for ($j=1; $j <= $total; $j++) {
$mail = $emails->get($j);
$post_array = array(
'post_content' => $mail['body'],
'post_title' => $mail['header']->subject,
'post_type' => 'my-custom-post-type',
'post_status' => 'publish',
'meta_input' => array(
'from' => $mail['header']->fromaddress,
'email_date'=> $mail['header']->Date, // add post meta as many as you want
'ticket_id' => $mail['header']->message_id,
),
);
wp_insert_post($post_array);
}
Now, $emails
is where I stored all the emails.
I want to do a simple check that if the emails already exist do not do wp_insert_post()
and if the email does not exist then only do wp_insert_post
.
I don't much about logic and how to do it. If someone can do it. It will be a big help.
body + subject + fromaffress + date + message_id
? justmessage_id
?wp_insert_post
message_id
is what I need to check if it already exists.