custom post types – See if the email exists or not


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,

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.

Rohit KC 1 month 2021-12-15T00:04:23-05:00 0 Answers 0 views 0

Leave an answer