2017-06-13 24 views
1

私はカスタムポストタイプのcrmを持っています。私は各crmが保存または更新された後にメールを送信する必要があります。私私の場合(WordPressのコーデックスに応じて)保存ユーザーなどの対象のようないくつかのカスタムメタ用のユーザーCMB2、私はポストの後save_postフック火災を知っ私は2つのパラメータ(IDおよびポスト)とsave_postを呼び出すポスト更新値は含まれません。ここに私のコードは次のとおりです。すべての投稿データと投稿メタデータを保存した後、どの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_postpost_updatedフックも試しましたが、更新データを取得することはできません。

どうすれば解決できますか?どのアクションフックが私にすべての新しいデータを与えますか?ありがとうございます。

答えて

0

を与える100に変更します。あなたはこのようなものを使用することができます https://codex.wordpress.org/Plugin_API/Action_Reference/post_updated

+0

私のために働いていない、それは更新ポストタイトルとpost_contentを与えますが、ポストメタを更新しません –

+0

この文脈では、 '$ post_after'は' WP_Post'クラスのインスタンスなので、カスタムポストメタは含まれません。 あなたの関数で、 '$ meta = get_post_meta($ crm-> ID);'を使ってすべてのポストメタフィールドを取得します。 –

+0

ええ、私はすでにそれを行っていますが、問題は、私が得た 'メタ値'は、 'post_updated'が発生した後のおそらくcmb2の更新メタ値です。私はそれを推測しているだけです。 –

1

この機能は、save_postフックを使用できます。あなたのフックの優先順位は、それがpost_updatedで試してみて、$post_afterオブジェクトを使用しますが、更新後の

add_action('save_post', 'send_mail_to_user', 100, 2); 
+0

うん、それはポストのためだけのメタを投稿していない、私はデータを更新できます。 –

2

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); 
関連する問題