2017-07-04 3 views
0

before_delete_postを使用して別のテーブルからデータを削除しようとしています。しかし、私のスクリプトは動作しません。私のbefore_delete_postはコードの先頭にあります。投稿を削除するときに得たい効果は、同じIDを持つ別のテーブル(wp_used_cars)からデータを削除することです。他のテーブルからデータを削除するには - WordPress

add_action('before_delete_post', 'my_func'); 
function my_func($postid){ 

    global $post_type; 
    if ($post_type != 'my_custom_post_type') return; 

    $post_type->query($wpdb->prepare('DELETE FROM wp_used_cars WHERE post_id = %d', $postid)); 
} 


// ------------------------------------------------------------------------- 
// add custom posts -------------------------------------------------------- 

// add uzywane custom post and set custom capabilities 
$uzywane = new CustomPost('uzywane'); 
$uzywane->make('Uzywane', 'Uzywane', 'Uzywane', array(
    'capabilities' => array(
     'edit_post'    => 'edit_used_car', 
     'read_post'    => 'read_used_car', 
     'delete_post'   => 'delete_used_car', 
     'edit_posts'   => 'edit_used_cars', 
     'edit_others_posts'  => 'edit_others_used_cars', 
     'publish_posts'   => 'publish_used_cars', 
     'read_private_posts' => 'read_private_used_cars', 
    ), 
)); 

// add this custom capabilities to admin 
$role = get_role('administrator'); 
$role->add_cap('edit_used_car', true); 
$role->add_cap('read_used_car', true); 
$role->add_cap('delete_used_car', true); 
$role->add_cap('edit_used_cars', true); 
$role->add_cap('edit_others_used_cars', true); 
$role->add_cap('publish_used_cars', true); 
$role->add_cap('read_private_used_cars', true); 

// if role uzywane don't exists add it and set capabilities to edit uzywane 
if(! role_exists('uzywane')) { 
    add_role(
     'uzywane', 
     __('Używane'), 
     array(
      'read'     => true, 
      'edit_used_car'   => true, 
      'read_used_car'   => true, 
      'delete_used_car'  => true, 
      'edit_used_cars'  => true, 
      'edit_others_used_cars' => true, 
      'publish_used_cars'  => true, 
      'read_private_used_cars'=> true, 
      'unfiltered_upload'  => true, 
      'upload_files'   => true, 
      'edit_posts'   => true, 
     ) 
    ); 
} 

// if user has role uzywane than remove links from admin panel 
$user = wp_get_current_user(); 
if (in_array('uzywane', (array) $user->roles)) { 
    function remove_links() { 
     echo '<style>.menu-icon-post, .menu-icon-promo_posts, .menu-icon-serwis, .menu-icon-nowe, .menu-icon-silniki, .menu-icon-comments, #toplevel_page_wpcf7, .menu-icon-tools, #wp-admin-bar-new-content, #wp-admin-bar-wpseo-menu, #wp-admin-bar-comments, .update-nag {display: none;}</style>'; 
    } 
    add_action('admin_head', 'remove_links'); 
} 


// if role uzywane don't exists add it and set capabilities to edit uzywane 
if(! role_exists('uzywane-admin')) { 
    add_role(
     'uzywane-admin', 
     __('Używane-admin'), 
     array(
      'read'     => true, 
      'edit_used_car'   => true, 
      'read_used_car'   => true, 
      'delete_used_car'  => true, 
      'edit_used_cars'  => true, 
      'edit_others_used_cars' => true, 
      'publish_used_cars'  => true, 
      'read_private_used_cars'=> true, 
      'unfiltered_upload'  => true, 
      'upload_files'   => true, 
      'edit_posts'   => true, 
      'edit_others_posts'  => true, 
      'manage_options'  => true, 
     ) 
    ); 
} 

// if user has role uzywane than remove links from admin panel 
$user = wp_get_current_user(); 
if (in_array('uzywane-admin', (array) $user->roles)) { 
    function remove_links() { 
     echo '<style> #toplevel_page_revslider, #toplevel_page_revslider, #menu-settings, #toplevel_page_wpseo_dashboard, #toplevel_page_edit-post_type-search-filter-widget, #toplevel_page_easy-responsive-tabs, .menu-icon-post, .menu-icon-promo_posts, .menu-icon-serwis, .menu-icon-nowe, .menu-icon-silniki, .menu-icon-comments, #toplevel_page_wpcf7, .menu-icon-tools, #wp-admin-bar-new-content, #wp-admin-bar-wpseo-menu, #wp-admin-bar-comments, .update-nag, #toplevel_page_edit-post_type-acf-field-group .wp-submenu li, #menu-appearance, .acf-columns-2 .wp-list-table.widefat tr, #acf-field-group-locations, #mymetabox_revslider_0, #acf-field-group-options {display: none;} .wp-list-table.widefat tr#post-456 {display: block };</style>'; 
    } 
    add_action('admin_head', 'remove_links'); 
} 


function cat_for_used_cars() { 

    $labels = array(
     'name'      => _x('Kategorie samochodów', 'Taxonomy General Name', 'text_domain'), 
     'singular_name'    => _x('Kategoria samochodów', 'Taxonomy Singular Name', 'text_domain'), 
     'menu_name'     => __('Kategoria samochodów', 'text_domain'), 
     'all_items'     => __('Wszystkie samochodów', 'text_domain'), 
     'parent_item'    => __('Parent Item', 'text_domain'), 
     'parent_item_colon'   => __('Parent Item:', 'text_domain'), 
     'new_item_name'    => __('Nazwa', 'text_domain'), 
     'add_new_item'    => __('Dodaj', 'text_domain'), 
     'edit_item'     => __('Edycja', 'text_domain'), 
     'update_item'    => __('Update Item', 'text_domain'), 
     'view_item'     => __('View Item', 'text_domain'), 
     'separate_items_with_commas' => __('Separate items with commas', 'text_domain'), 
     'add_or_remove_items'  => __('Add or remove items', 'text_domain'), 
     'choose_from_most_used'  => __('Choose from the most used', 'text_domain'), 
     'popular_items'    => __('Popular Items', 'text_domain'), 
     'search_items'    => __('Search Items', 'text_domain'), 
     'not_found'     => __('Not Found', 'text_domain'), 
     'no_terms'     => __('No items', 'text_domain'), 
     'items_list'     => __('Items list', 'text_domain'), 
     'items_list_navigation'  => __('Items list navigation', 'text_domain'), 
    ); 
    $args = array(
     'labels'      => $labels, 
     'hierarchical'    => true, 
     'public'      => true, 
     'show_ui'     => true, 
     'show_in_menu'    => true, 
     'show_admin_column'   => true, 
     'show_in_nav_menus'   => false, 
     'show_tagcloud'    => false, 
     'rewrite'     => false, 
    ); 
    register_taxonomy('usedcars', array('uzywane'), $args); 

} 
add_action('init', 'cat_for_used_cars', 0); 

答えて

1

コードの下には試してみてください。

add_action('before_delete_post', 'my_func'); 
function my_func($postid) 
{ 
    global $post_type; 
    if ($post_type != 'uzywane') 
     return; 

    global $wpdb; 
    $prefix = $wpdb->prefix; 
    $tbl = $prefix.'used_cars'; 
    $used_cars_count = $wpdb->get_var("SELECT COUNT(*) FROM $tbl where post_id = ". $postid); 
    if($used_cars_count > 0) 
    { 
     $wpdb->delete($tbl, array('post_id' => $postid)); 
    } 
} 

希望これはあなたを助けます!

+0

それは仕事です!どうもありがとうございました! – paciek

+0

あなたはようこそ! –

関連する問題