您可以使用transition_post_status
钩子,每当状态更改时都会激发它。因此,当状态更改为publish
, 您可以这样做:
function notificationApprove( $new_status, $old_status, $post ) {
if ( \'publish\' === $new_status && \'publish\' !== $old_status && \'post\' === $post->post_type ) {
// Send mail
}
}
add_action( \'transition_post_status\', \'notificationApprove\', 10, 3 );
这将仅在状态更改时激发。这意味着,如果帖子未发布,然后重新发布,则会再次发送通知。
如果只发送一次通知,则可以在发送通知时保存自定义字段,然后在重新发布帖子时检查该字段是否已存在,如果存在,则不发送通知:
function notificationApprove( $new_status, $old_status, $post ) {
if ( \'publish\' === $new_status && \'publish\' !== $old_status && \'post\' === $post->post_type ) {
$notification_sent = get_post_meta( $post->ID, \'notification_sent\', true );
if ( $notification_sent !== \'sent\' ) {
// Send mail
update_post_meta( $post->ID, \'notification_sent\', \'sent\' );
}
}
}
add_action( \'transition_post_status\', \'notificationApprove\', 10, 3 );