0

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.

4
  • @Rohic : Hi. I'm not sure I fully understand your problem. You only want to add the email if it doesn't already exist. What makes an email unique? body + subject + fromaffress + date + message_id? just message_id?
    – FS-GSW
    Dec 15, 2021 at 7:48
  • @Rohic : So, you need to create a function that returns true or false with the fields that make a post unique as parameters. This function will be used in the condition you wish to set up before the insertion. In this function, you need to do a simple query to retrieve the post with these field values. If a post is found, the function returns true, so no insert, otherwise the function returns false and you can do the wp_insert_post
    – FS-GSW
    Dec 15, 2021 at 8:08
  • For now just message_id is what I need to check if it already exists.
    – Rohit kc
    Dec 15, 2021 at 8:08
  • yes, that's kind of what I am trying to do. but I just don't know how to get started.
    – Rohit kc
    Dec 15, 2021 at 8:12

1 Answer 1

0

First, you need to create the following function:

function checkEmailAlreadyExists( $message_id ) {
    $posts_with_meta = get_posts( array(
        'posts_per_page' => 1, // we only want to check if any exists, so don't need to get all of them
        'post_type'      => 'my-custom-post-type',
        'meta_key'       => 'ticket_id',
        'meta_value'     => $message_id,
        'fields'         => 'ids', // we don't need it's content, etc.
    ) );

    return ( count( $posts_with_meta ) ) ? true : false;    
}

Now you just have to add it to your current code:

$total = $emails->total_msg(); 
for ( $j=1; $j <= $total; $j++ ) {
    $mail = $emails->get( $j );
    if ( !empty( $mail['header']->message_id ) && !checkEmailAlreadyExists( $mail['header']->message_id )  ) {
        $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,
                'ticket_id'  => $mail['header']->message_id,
            ),
        );
        wp_insert_post( $post_array );
    }
}
11
  • This is not quite working, as every time I refresh same emails are being inserted repeatedly.
    – Rohit kc
    Dec 15, 2021 at 8:35
  • @Rohic : I added the post_type parameter to the « check » function. Can you try doing a: $test = checkEmailAlreadyExists(XXX); replacing XXX with an existing DB message_id and then a var_dump($test);
    – FS-GSW
    Dec 15, 2021 at 8:41
  • For every message_id it is giving false.
    – Rohit kc
    Dec 15, 2021 at 8:51
  • I just tested on one of my sites, I did it with the post_type: post and it works perfectly. Did you add the post_type in the checkEmailAlreadyExists function?
    – FS-GSW
    Dec 15, 2021 at 8:56
  • Yes, I added my post type in checkEmailAlreadyExist but nothing happend.
    – Rohit kc
    Dec 15, 2021 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.