保存所有帖子数据和帖子元之后,哪个WordPress钩子会触发?

M. *_*ain 6 php wordpress wordpress-hook cmb2

我有一个自定义帖子类型crm,保存或更新每个crm后都需要发送一封邮件。我为某些自定义元(例如主题,用户等)使用cmb2,我知道在我使用两个参数(id和post)调用时,在save_post保存后(根据WordPress Codex)钩子触发了,该帖子不包含更新值。这是我的代码:save_post

function send_mail_to_user($id, $post){
    $crm = $post;
    $user_email = array();
    if($crm->vc_all_vc == 'on'){
        $args = array('orderby' => 'display_name');
        $wp_user_query = new WP_User_Query($args);
        $authors = $wp_user_query->get_results();
        if (!empty($authors)) {
            foreach ($authors as $author) {
                array_push($user_email , $author->user_email );
            }
        } 
    }
    else{
        $to_users = $crm->vc_users;
        $to_program = $crm->vc_program;
        $to_group = $crm->vc_group;
        $to_excode = $crm->vc_ex_code;
        foreach ($to_users as $key => $value) {
            $user_data = get_userdata($value);
            array_push($user_email, $user_data->user_email);
        }
        foreach ($to_program as $key => $value) {
            $users = get_users( array('meta_key'     => 'programs'  ) );
            if($users){ 
                foreach ($users as $index => $data) {
                    if(in_array($value , explode('#', $data->programs))){
                        if(! in_array($data->user_email, $user_email)  )
                        {
                            array_push($user_email, $data->user_email);
                        }
                    }
                }
            }
        }
        foreach($to_group as $group) {
            $term = get_term_by('slug', esc_attr($group), 'user-group');
            $user_ids = get_objects_in_term($term->term_id, 'user-group');
            foreach($user_ids as $user_id){
                $fc_user = get_userdata($user_id);
                if(! in_array($fc_user->user_email, $user_email)  )
                {
                    array_push($user_email, $fc_user->user_email);
                }
            }   
        }
        foreach($to_excode as $codes) {
            $value = explode('*',$codes)[1];
            $users = get_users( array('meta_key'     => 'programs'  ) );
            if($users){ 
                foreach ($users as $index => $data) {
                    if(in_array($value , explode('#', $data->programs))){
                        if(! in_array($data->user_email, $user_email)  )
                        {
                            array_push($user_email, $data->user_email);
                        }
                    }
                }
            }   
        }
    }
    foreach($user_email as $index => $email){
        $to      = $email;
        $subject = $crm->vc_subject;
        $body    = $crm->post_content;
        $headers = array(
        'Content-Type: text/html; charset=UTF-8'
        );
        wp_mail($to, $subject, $body, $headers);
    }
}

add_action( 'save_post', 'send_mail_to_user', 10, 2 ); 
Run Code Online (Sandbox Code Playgroud)

而且我还尝试了publish_posthook,当创建新帖子时工作正常,但在更新时也一样。我曾尝试edit_postpost_updated也勾,但我从来没有能够找回我的更新数据。

那么我该如何解决呢?哪个动作挂钩将为我提供所有新数据?提前致谢。

Abd*_*eed 7

你可以使用这样的东西,

function your_custom_function($meta_id, $post_id, $meta_key='', $meta_value='') {
    if($meta_key=='_edit_lock') {
        // if post meta is updated
    }
}
add_action('updated_post_meta', 'your_custom_function', 10, 4); 
Run Code Online (Sandbox Code Playgroud)


Oma*_*nti 5

这可能有点旧,但只是想提供更新,因为从版本 5.6.0 开始,有一个新的钩子可用。问题就在这里,您可以在这里wp_after_insert_post找到更多信息。创建或更新帖子并且其所有术语和元都更新后会触发此挂钩。您可以在下面找到一个示例:

add_action( 'wp_after_insert_post', 'send_mail_to_user', 90, 4 );

/**
 * Callback to: 'wp_after_insert_post'
 * Fires once a post, its terms and meta data has been saved
 * @param int     $post_id Post ID.
 * @param WP_Post $post    Post object.
 * @param bool    $update  Whether this is an existing post being updated.
 * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior to the update for updated posts.
 *
 */
public static function sync_product_registrations_on_update( $post_id, $post, $update, $post_before ) {

    if ( 'post' !== $post->post_type ) {
        //Only to process the below when post type is 'post' else return
        return;
    }

    if ( ! in_array( $post->post_status, [ 'private', 'publish' ] ) ) {
        //To only process when status is private or publish
        return;
    }

    //The rest of your code goes here


}

Run Code Online (Sandbox Code Playgroud)


小智 3

尝试post_updated并使用$post_after对象。 https://codex.wordpress.org/Plugin_API/Action_Reference/post_updated

  • 如果您使用高级自定义字段 (ACF),则常规 WP 挂钩(包括 pippas 答案中提到的 Rest_after_insert_{$this->post_type} 等较新的挂钩)将不起作用,您必须使用 ACF 挂钩 'acf/save_post' https://www.advancedcustomfields.com/resources/acf-save_post/ (2认同)