2014-10-17 67 views
0

嗨:)我的工作我的第一个插件.. 我已经注册了我的自定义后类型,以其元箱和分类.. 一切似乎工作正常,除了当我卸载插件时,我的uninstall.php中的函数不会删除帖子。WordPress的:wp_delete_post在uninstall.php不会删除我的自定义信息

这里我uninstall.php

 // If uninstall not called from WordPress, then exit. 
    if (! defined('WP_UNINSTALL_PLUGIN')) { 
     exit; 
    } 

    global $wpdb; 

    // Delete All Custom Type Posts 
    $posts = get_posts(array(
     'numberposts' => -1, 
     'post_type' => 'my-custom-post', // $post_type 
     'post_status' => 'any')); 

    foreach ($posts as $post) { 
     wp_delete_post($post->ID, true); 
    } 

我到处 - 即使里面其他插件 - 它似乎这是做正确的方式...有人能告诉我为什么不起作用?或者即使有其他方式?

+0

你的代码看起来不错,你怎么称之为uninstall.php? – 2014-10-17 11:20:44

+0

感谢您的回复:) 我认为这足以将uninstall.php放入插件的根目录中。 我在某处读过这个东西,他们在这里也这样说: [https://stackoverflow.com/问题/ 19900769/how-to-call-uninstall-php](https://stackoverflow.com/questions/19900769/how-to-call-uninstall-php) 但它仍然不适用于我.. 。其他想法? – supergiu 2014-10-17 12:47:20

+0

我测试了你的代码并且工作正常。你确定邮政类型名称吗?在你的'get_posts'后添加以下内容以确保它正常工作:'wp_die(sprintf('

%s
',print_r($ posts,true)));' – brasofilo 2014-10-17 22:05:04

回答

0

从抄本,

register_uninstall_hook注册卸载钩子,当用户点击调用该插件卸载本身的卸载链接将被调用。

register_uninstall_hook($file, $callback) 

check this link

0

好吧,我调查了一下...... 阅读这里: https://wordpress.stackexchange.com/questions/98965/get-posts-from-sites-in-multisite

这里:https://wordpress.stackexchange.com/questions/25910/uninstall-activate-deactivate-a-plugin-typical-features-how-to/25979#25979

然后我就暂时代替get_posts()用新的WP_Query(),相同的参数,然后我以这种方式打印查询:

$args = array(
    'numberposts' => -1, 
    'post_type' => 'my-custom-post', 
    'post_status' => 'any' 
); 
$posts = new WP_Query($args); 

wp_die(sprintf('<pre><code>%s</code></pre>', print_r($posts->request, true))); 

这个结果:

SELECT SQL_CALC_FOUND_ROWS _wpposts.ID FROM _wpposts WHERE 1=1 AND _wpposts.post_type = 'my-custom-post' AND (_wpposts.post_status = 'publish' OR _wpposts.post_status = 'future' OR _wpposts.post_status = 'draft' OR _wpposts.post_status = 'pending' OR _wpposts.post_status = 'private') ORDER BY _wpposts.post_date DESC LIMIT 0, 10 

我觉得这里的问题是,插件是活动的多站点网络上,但仅在该网站(ID为2)之一有与我的$ post_type帖子... 所以我试图通过使用switch_to_blog()使用此功能的博客做一个循环:

function my_uninstall_plugin() { 
    global $wpdb; 
    if (function_exists('is_multisite') && is_multisite()) { 
     $blog_sql = "SELECT blog_id FROM $wpdb->blogs WHERE archived = '0' AND spam = '0' AND deleted = '0'"; 
     $blog_ids = $wpdb->get_col($blog_sql); 

     if ($blog_ids) { 
      foreach ($blog_ids as $blog_id) { 
       switch_to_blog($blog_id); 

       global $post; 
       $args = array(
        'numberposts' => -1, 
        'post_type' => 'un-custom-post', 
        'post_status' => 'any' 
       ); 
       $blog_posts = get_posts($args); 
       if($blog_posts) { 
        foreach ($blog_posts as $blog_post) { 
         wp_delete_post($blog_post->ID, true); 
        } 
       } 
      } 
      restore_current_blog(); 
     } 
    } 
} 

而现在它似乎工作... 感谢Vidhu奈尔和brasofilo对你有所帮助! :)