0

I have a function that sends out emails using wp_mail() every time a post is published or updated. However, the content that is being sent is always one post behind.

If I update the content the email sends the previous content before the update. I am not sure how to fix. I appreciate any help you can give, thanks.

function send_media_emails($post_id){
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
        return;
    if(get_post_status($post_id) == 'draft' or get_post_status($post_id) == 'pending' or get_post_status($post_id) == 'trash')
        return;
    $to  = 'email@email.com';
    $subject = 'My Email Subject';
    $post = get_post();

    $body = '<h1>'.$post->post_title.'</h1>';   
    if(is_category){
        $category = get_the_category();
        $body .= '<h2>Reason for Closure: <em>'.$category[0]->cat_name .'</em></h2>';
    }
    $body .= '<h3>This Cancellation/Delay was posted on '.$post->post_date.'</h3>';
    $body .= '<p>'.$post->post_content.'</p>';
    $body .= '<p>View this posting at ' . get_permalink($post_id) . ' or</p>';

    if(did_action('post_updated') == 1){
        wp_mail($to, $subject, $body);
    }
}
add_action('post_updated', 'send_media_emails');
3

There's a few things you need to do here, you need to modify your add_action() to accept additional arguments and you need to specify whether to use the data before the update or the data after the update.

Try something like this:

function send_media_emails($post_id, $post_after){
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
        return;
    if(get_post_status($post_id) == 'draft' or get_post_status($post_id) == 'pending' or get_post_status($post_id) == 'trash')
        return;
    $to  = 'email@email.com';
    $subject = 'My Email Subject';
    $post = $post_after; // NOTE We're using the after post update data here

    $body = '<h1>'.$post->post_title.'</h1>';   
    if(is_category){
        $category = get_the_category();
        $body .= '<h2>Reason for Closure: <em>'.$category[0]->cat_name .'</em></h2>';
    }
    $body .= '<h3>This Cancellation/Delay was posted on '.$post->post_date.'</h3>';
    $body .= '<p>'.$post->post_content.'</p>';
    $body .= '<p>View this posting at ' . get_permalink($post_id) . ' or</p>';

    if(did_action('post_updated') == 1){
        wp_mail($to, $subject, $body);
    }
}
add_action('post_updated', 'send_media_emails', 10, 2); 
// NOTE we've added 2 arguments to be accepted and also a priority of 10

More information on the add_action() arguments can be found here and you can see an example of the before and after post data on the post_updated action here

  • Thanks for your reply Craig, It worked like a charm. I am a little foggy on one thing though. Where are my post variable being set if not in $post = get_post(); that is now $post = $post_after as I use the $post variable to grab all the content. Sorry if that is a dumb question. – bilcker Feb 6 '15 at 18:22
  • No it's not a dumb question, this is set in the "function" post_updated all we're doing when using add_action is saying "Hey, I want to hook into an existing function and I want access to all the variables in that function". And if you were to find post_updated in the wordpress core you'd see that it stores data in the variables: $post_id, $post_after and $post_before. In this case this function is found in wp-includes\post.php - Which you should never modify because it's a core file – Craig Pearson Feb 6 '15 at 18:32
  • Makes sense, Thanks for explaining and helping me out, greatly appreciated. – bilcker Feb 6 '15 at 18:34
  • To clarify: post_updated sets $post_after like so within the WordPress core: $post_after = get_post($post_id); – Craig Pearson Feb 6 '15 at 18:35
  • I just noticed that after I used the post_after, my $post->post_title no longer appears unless I update the post. I tried adding the post_before thinking it would return but no luck. – bilcker Feb 6 '15 at 19:45

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.