简体   繁体   English

wordpress - wp 插入后挂钩仅用于保存新职位

[英]wordpress - wp insert post hook for only new posts saved

I am using wp insert post as a hook to send an email when ever a new post has been submitted in wordpress, I have taken the reference of this link and tried below code.当在 wordpress 中提交了新帖子时,我使用wp insert post作为挂钩来发送 email,我参考了此链接并尝试了以下代码。 I am able to get emails perfectly, the problem is I am receiving mails even when the post is trashed which is not required.我能够完美地收到电子邮件,问题是即使帖子被丢弃,我也会收到邮件,这不是必需的。 Is there any way to trigger mail only when new post is created and not for any other actions.有没有办法仅在创建新帖子而不是任何其他操作时触发邮件。

function my_project_updated_send_email( $post_id, $post, $update ) {

// If this is a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) )
    return;

$post_url = get_permalink( $post_id );
$subject = 'A post has been updated';

$message = "A post has been updated on your website:\n\n";
$message .= $post->post_title . ": " . $post_url;

// Send email to admin.
wp_mail( 'admin@example.com', $subject, $message );
}
add_action( 'wp_insert_post', 'my_project_updated_send_email', 10, 3 );

I just figured it out just after posting this question, but updating my solution here so that it would be useful for some one. 我刚在发布此问题后才弄清楚了,但是在这里更新了我的解决方案,以使其对某些人有用。

The solution is I have added a checkpoint by checking the post_status of submitted post using the same wp insert post like below. 解决的办法是我使用如下相同的wp insert post通过检查提交帖子的post_status添加了一个检查点。

$post_status = get_post($post_id)->post_status;

if($post_status == 'pending'){
//send the post pending email
}elseif($post_status == 'publish'){
// send the post published email
}elseif($post_status == 'trash'){
// send the post trashed email
}

You can use Post Status Transitions . 您可以使用“ 发布状态转换” Example for draft -> publish below. draft -> publish示例draft -> publish下面draft -> publish

add_action('draft_to_publish', 'draft_to_publish_actions');
function draft_to_publish_actions($object)
{
//do stuff    
}
function wcl_insert_car($post_ID, $post, $update) {
    if (get_post_type() != 'car') {
        return;
    }
    $value = get_post_meta($post_ID, 'car_init', true);
    if (empty($value)) {
        // One time code on init Post
        update_post_meta($post_ID, 'car_init', true);
    }
}
add_action('wp_insert_post', 'wcl_insert_car', 10, 3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WordPress试图将wp_insert_post用于新帖子,将wp_update_post用于帖子更新-是否需要帖子ID? - Wordpress trying to use wp_insert_post for new posts and wp_update_post for post updates - need post ID? 如果条件为wp_insert_post_data,则Wordpress禁止保存帖子 - Wordpress prevent post to be saved if condition is true with wp_insert_post_data PHP WP如果只有一个wp_insert_post,则要创建两个帖子? - PHP WP For create two posts when theres only one wp_insert_post? wordpress:无法挂接wp_insert_term - wordpress: failed to hook wp_insert_term 如何使用wp_insert_post和SQL查询填充Wordpress帖子 - How to populate Wordpress Posts using wp_insert_post and SQL query Wordpress wp_insert_post 在创建新的自定义帖子时正在创建新的分类术语 - Wordpress wp_insert_post is creating new Taxonomy term when creating new custom post 简码中的WP_Query和posts_per_page仅返回一篇文章(WordPress) - WP_Query and posts_per_page from shortcode only returning one post (WordPress) Wordpress:显示错误消息 - 钩子admin_notices在wp_insert_post_data或publish_post上失败 - Wordpress: displaying an error message - hook admin_notices fails on wp_insert_post_data or publish_post 试图在 wordpress 帖子的回应中插入帖子永久链接 - Trying to insert post permalink in echo of the wordpress posts Wordpress wp_insert_post功能不起作用 - Wordpress wp_insert_post function not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM