我正在制作一个自定义通知,在投稿人的帖子计划发布时发送给投稿人,共享计划发布日期和各种其他信息(标题、类别等)。
我遇到的问题是,如果我打开一篇待处理的帖子,将日期设置为未来,然后点击时间表,通知中不包括帖子的预定发布日期,而是包括帖子的原始提交日期。
我假设问题是,我的功能在数据库更新为帖子的新日期和其他更新信息之前启动。代码如下-非常感谢您的帮助!
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 <test@testing.com>\';
   $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\');
 
                    最合适的回答,由SO网友:gmazzap 整理而成
                    而不是依赖全球$post, 您应该使用通过posts状态转换传递给函数的post对象:
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
}