wordpress:wp#u delete#u uninstall.php中的post不';不要删除我的自定义帖子
嗨:)我正在开发我的第一个插件。。 我已经注册了我的自定义帖子类型,以及它的元框和分类法。。 除了卸载插件时,我的uninstall.php中的函数不会删除帖子外,一切似乎都很正常 这里是我的卸载.phpwordpress:wp#u delete#u uninstall.php中的post不';不要删除我的自定义帖子,wordpress,Wordpress,嗨:)我正在开发我的第一个插件。。 我已经注册了我的自定义帖子类型,以及它的元框和分类法。。 除了卸载插件时,我的uninstall.php中的函数不会删除帖子外,一切似乎都很正常 这里是我的卸载.php // If uninstall not called from WordPress, then exit. if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { exit; } global $wpdb
// 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 );
}
我到处都找过了——甚至在其他插件里面——似乎这是正确的方法……有人能告诉我为什么它不起作用吗?或者,即使有其他方法?从法典中
register\u uninstall\u hook
注册卸载钩子,当用户单击调用插件卸载自身的卸载链接时,将调用该钩子
register_uninstall_hook($file, $callback)
好的,我已经调查了一下。。。 在这里阅读: 在这里: 然后,我临时将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 ) ) );
,print_r($posts->request,true));
因此:
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();
}
}
}
我认为这里的问题是,插件在多站点网络上是活动的,但只有在其中一个站点(ID为2)中有我的$post_类型的帖子。。。
因此,我尝试使用switch_to_blog()
函数在博客中循环:
现在它似乎起作用了。。。
感谢维杜奈尔和布拉索菲洛的帮助!:) 您的代码看起来不错,如何调用uninstall.php?感谢您的回复:)我认为将uninstall.php放在插件的根目录中就足够了。。我在某个地方读到这篇文章,他们在这里也这么说:但这对我来说仍然不起作用。。。还有其他想法吗?我测试了你的代码,效果很好。您确定帖子类型名称吗?在
get_posts
之后添加以下内容,以确保其正常工作:wp_die(sprintf(“”,print_r($posts,true))你好,布拉索菲洛!谢谢你的建议,很抱歉我花了一段时间才回复。。。我尝试了你的代码,它返回一个空数组。。。我想知道为什么?$post_type
是正确的,如果我尝试连接到停用挂钩的相同功能,它将完美工作。。。也许我需要检查一下多站点网络?我试着去做,也许有用。。。与此同时,你还有别的想法吗?
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();
}
}
}