0

I'm working on a custom notification to be sent to contributors when their post is scheduled for publication, sharing the scheduled date of publication and various other information (title, category, etc).

The problem I'm having is that if I open up a pending post, set the date to the future, and then hit schedule, the notification doesn't include the post's scheduled publication date, it includes the post's original submission date.

I assume the issue is that my function is firing before the database is updated with the post's new date and other updated information. Code is below - any help is much appreciated!

function scheduledNotification() {
   global $post;
   $post = get_post($post_id);
   $author = get_userdata($post->post_author);
   $link = get_permalink($post->ID);
   $date = mysql2date('M j Y', $post->post_date);
   $category = get_the_category($post->ID);
   $category = $category[0]->cat_name;

   $message = "<p>Dear ".$author->user_firstname.",</p>".           
              "<p>We will be publishing your ".$category." article, ".$post->post_title.", on ".$date.".</p>";  

   $headers[] = 'From: Send Name <[email protected]>';
   $headers[] = 'Content-type: text/html';
   wp_mail($author->user_email, "Your Upcoming Post", $message, $headers);
}

add_action('pending_to_future', 'scheduledNotification');
add_action('approved_to_future', 'scheduledNotification');

1 Answer 1

0

Instead of rely on global $post, you should use the post object passed to function via the posts status transition:

add_action('pending_to_future', 'scheduledNotification');
add_action('approved_to_future', 'scheduledNotification');

function scheduledNotification( $post ) {
   $author = get_userdata($post->post_author);
   // the rest of your function here
}

Your Answer

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

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