2016-09-07 11 views
1

私はプラグインで関数を作っています。その関数は、投稿がゴミ箱に移動したときにデータベース行を削除します。しかし、get_posts()を使ってpost_idを取得できません。投稿をすべて削除するにはどうすればいいですか? Wordpress

function delete_condition($post) 
{ 
    global $wpdb; 

     $allposts = get_posts(array(
     'numberposts' => -1, 
     'category' => 0, 'orderby' => 'date', 
     'order' => 'DESC', 'include' => array(), 
     'exclude' => array(), 'meta_key' => '', 
     'meta_value' =>'', 'post_type' => 'job', 
     'suppress_filters' => true)); 

     foreach($allposts as $postinfo) { 
      $wpdb->delete('rule', array('post_id' => $postinfo)); 
     } 

} 
add_action('wp_trash_post', 'delete_condition', 10, 1); 

おかげ

答えて

1

アクションフックをあなたは、ここにwp_trash_postを使用している、パラメータとして関数への$ post_idのを渡します。参照してください:https://codex.wordpress.org/Plugin_API/Action_Reference/trash_post

ごみ箱と同じ投稿IDを持つ1つの表からすべての行を削除するように思えます。

私はあなたがこのような何かを書きたいかもしれないと思う:

function delete_condition($post_id) { 
global $wpdb; 
// Delete rows in the rule table which have the same post_id as this one 
if ('job' === get_post_type($post_id)) { 
    $wpdb->delete('rule', array('post_id' => $post_id)); 
} 
} 

add_action('wp_trash_post', 'delete_condition', 10, 1); 
0

$ postinfoオブジェクトです:

は、ここに私のコードです。あなたは投稿のIDだけを必要とします。ですから、$ postinfo-> IDと書くべきです。以下に、あなたのループを交換してください -

foreach($allposts as $postinfo) { 
      $postinfoID = $postinfo->ID; 
      $wpdb->delete('rule', array('post_id' => $postinfoID)); 
    } 
0
<?php 
function delete_condition($post) 
{ 
    global $wpdb; 

     $allposts = get_posts(array(
     'numberposts' => -1, 
     'post_status' => 'any', 
     'category' => 0, 'orderby' => 'date', 
     'order' => 'DESC', 'include' => array(), 
     'exclude' => array(), 'meta_key' => '', 
     'meta_value' =>'', 'post_type' => 'job', 
     'suppress_filters' => true)); 

     foreach($allposts as $postinfo) { 
      $wpdb->delete('rule', array('post_id' => $postinfo)); 
     } 

} 
add_action('wp_trash_post', 'delete_condition', 10, 1); 
?> 
関連する問題