0

I make a custom function so when postmeta updated to specific value i send custom email use wp_mail. email successfully send but it create twice at same time.

here my code

function email_notification_by_status()
{

// Global Variable

$post_id = get_The_ID();
$user = wp_get_current_user();
$user_id = get_current_user_id();
$blog_id = get_current_blog_id();
$dd_status = get_post_meta($post_id, 'screening_status', true);
$dd_analyst = get_post_meta($post_id, 'select_analyst', true);

//Completed

if ('Completed' == $dd_status)
    {

    $args = array(
        'role' => 'client',
        'blog_id' => $blog_id
    );
    $client_infos = get_users($args);
    foreach($client_infos as $client_info)
        {
        $client_email = $client_info->user_email;
        $to = $client_email;
        $subject = 'Due diligence is Complete';
        $body = 'Lorem ipsum';
        wp_mail($to, $subject, $body);
        }
    }
}

add_filter('updated_postmeta', 'email_notification_by_status');

could please tell me what's wrong with my code. thanks

0

remove foreach and directly add this code

$client_infos = get_users($args);
if($client_infos)
{
    $client_email = $client_info[0]->user_email;
    $to = $client_email;
    $subject = 'Due diligence is Complete';
    $body = 'Lorem ipsum';
    wp_mail($to, $subject, $body);
}
  • It's not a very good idea. Your code will cause problems, if there is no user found... – Krzysiek Dróżdż Apr 16 '18 at 10:10
  • it's code perfect working if user found and no any kind of problem in it if user not found – nikhil gadhiya Apr 17 '18 at 9:10
  • well, yes. After you’ve edited it ;) – Krzysiek Dróżdż Apr 17 '18 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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