批准的答案对我不起作用。我最后尝试了几个条件,但电子邮件仍会被发送两次:
function xxx_send_mail($id, $post, $update){  
                if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) {
                    return;
                }
                if (wp_is_post_revision($id)) {
                    return;
                }
                if (wp_is_post_autosave($id)) {
                    return;
                }
                // if new post
                if (!$update) {
                    return;
                }
                wp_mail(\'john@doe.com\', \'The subject\', \'The message\');
         }
add_action( \'save_post_{the_post_type}\', \'xxx_send_mail\', 10, 3 );
 我最终通过添加一个由WP提供的方便函数来解决这个问题,该函数名为
did_action():
    function xxx_send_mail($id, $post, $update){  
                        if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) {
                            return;
                        }
                        if (wp_is_post_revision($id)) {
                            return;
                        }
                        if (wp_is_post_autosave($id)) {
                            return;
                        }
                        // if new post
                        if (!$update) {
                            return;
                        }
                $times = did_action(\'save_post_{the_post_type}\');
                if( $times === 1){
                        wp_mail(\'john@doe.com\', \'The subject\', \'The message\');
                 }
    }
add_action( \'save_post_{the_post_type}\', \'xxx_send_mail\', 10, 3 );
 希望这对某人有帮助