2017-09-13 11 views
0

私は、 'fep_message'タイプのポストが保存されたときにpost_parent IDに関連付けられた '_fep_delete_by_'キーをチェックするようWordpressでadd_actionを試行しています。 wp_post_metaテーブルから削除します。これを行うために作成したコードですが、動作しません。ポストタイプが保存されたときに実行する

add_action('publish_post', 'undelete_thread'); 
function undelete_thread($post_id, $post) { 
    global $wpdb; 
    if ($post->post_type = 'fep_message'){ 
     $participants = fep_get_participants($post->post_parent); 
     foreach($participants as $participant) 
     { 
      $query ="SELECT meta_id FROM wp_postmeta WHERE post_id = %s and `meta_key` = '_fep_delete_by_%s'"; 
      $queryp = $wpdb->prepare($query, array($post->post_parent, $participant)); 
      if (!empty($queryp)) { 
       delete_post_meta($queryp,'_fep_delete_by_' . $participant); 
      } 
     } 
    } 
} 

これを行うには適切なフックはありますか?

答えて

0

wordpressのsave_postフックを使用してください。

add_action('save_post', 'undelete_thread'); 

function undelete_thread($post_id) { 
    global $wpdb; 
    global $post; 

    if ($post->post_type = 'fep_message'){ 
     $participants = fep_get_participants($post->post_parent); 
     foreach($participants as $participant) 
     { 
      $query ="SELECT meta_id FROM wp_postmeta WHERE post_id = %s and `meta_key` = '_fep_delete_by_%s'"; 
      $queryp = $wpdb->prepare($query, array($post->post_parent, $participant)); 
      if (!empty($queryp)) { 
       delete_post_meta($queryp,'_fep_delete_by_' . $participant); 
      } 
     } 
    } 
} 
0

おかげでクリスは、あなたが正しい、save_post作品です:あなたは、コードは、このように変更する必要があり、ここで

https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

より多くの情報を見つけることができます。私は機能をより単純化しました。これはうまくいきます:

add_action('save_post', 'undelete_thread'); 
function undelete_thread($post_id) { 
    $post = get_post($post_id); 
    if ($post->post_type = 'fep_message'){ 
     $participants = fep_get_participants($post->post_parent); 
     foreach($participants as $participant)   
     { 
      delete_post_meta($post->post_parent,'_fep_delete_by_'. $participant);  
     } 
    } 
} 
関連する問題