我有一個自定義的帖子類型crm,並且我需要在每個crm保存或更新後發送郵件。我知道save_post
鉤後發生保存(根據WordPress代碼)在我的情況下,當我撥打save_post
與兩個參數(編號和職位)該職位不包含更新值。這裏是我的代碼:保存所有後期數據併發布meta後,哪個WordPress鉤子會被觸發?
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);
,我也嘗試publish_post
掛鉤,新的崗位創建時工作正常,但在更新時它的工作原理相同。我試過edit_post
和post_updated
掛鉤,但我永遠無法檢索我的更新數據。
那麼我該如何解決呢?哪個動作鉤子會給我所有的新數據? 在此先感謝。
不適用於我,它給出更新帖子標題和post_content,但不更新帖子meta –
在這種情況下'$ post_after'是'WP_Post'類的一個實例,所以它不包括自定義帖子元。 使用'$ meta = get_post_meta($ crm-> ID);'在你的函數中獲取所有後期元字段。 –
是的,我已經這樣做了,但是問題是'meta value'我得到的不是更新版本,可能是'post_updated'後cmb2更新'meta values'觸發。我只是在猜測。 –