2017-09-13 36 views
0

我想在Wordpress中添加一個action,這樣當保存類型'fep_message'的帖子時,我檢查與post_parent ID關聯的任何'_fep_delete_by_'鍵以及然後我從wp_post_meta表中刪除它們。這是我建立的代碼,但它不工作:在保存後類型時運行在wordpress中運行一個函數

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鉤子。您可以在這裏找到

https://codex.wordpress.org/Plugin_API/Action_Reference/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作品。我更多地簡化了功能,並且這個工作正常:

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);  
     } 
    } 
}