admin管理员组文章数量:1422437
I have just started learning WP coding . So , I am trying to delete all custom posts on uninstall of my plugin . I have created uninstall.php in my plugin root and have the following :
<?php
if(!defined(WP_UNINSTALL_PLUGIN)){
die();
}
// Delete Database
$books= get_posts(['post_type'=>'book','numberposts'=>-1]);// all posts
foreach($books as $book){
wp_delete_post($book->ID,true);
}
On uninstall I get an error 'Deletion Failed'. Could somebody tell me whats going wrong ? Also , how do you debug this sort of issues in WP ? I enabled in my wp-config the following
define('WP_DEBUG', true);
define( 'WP_DEBUG_LOG', true );
but I do not see any helpful debug message other than "Deletion Failed' in the plugin screen nor a debug.log file in my wp-content after enabling the above two .
I also tried deleting using SQL like below :
global $wpdb;
$wpdb->query("DELETE FROM wp_posts WHERE post_type='book'");
$wpdb->query("DELETE from wp_postmeta WHERE post_id NOT IN( SELECT id FROM wp_posts");
Even this gives me Deletion Failed Error . After , deactivation I see the custom posts with the post_type in my database and I have no clue why this is not working .
I have just started learning WP coding . So , I am trying to delete all custom posts on uninstall of my plugin . I have created uninstall.php in my plugin root and have the following :
<?php
if(!defined(WP_UNINSTALL_PLUGIN)){
die();
}
// Delete Database
$books= get_posts(['post_type'=>'book','numberposts'=>-1]);// all posts
foreach($books as $book){
wp_delete_post($book->ID,true);
}
On uninstall I get an error 'Deletion Failed'. Could somebody tell me whats going wrong ? Also , how do you debug this sort of issues in WP ? I enabled in my wp-config the following
define('WP_DEBUG', true);
define( 'WP_DEBUG_LOG', true );
but I do not see any helpful debug message other than "Deletion Failed' in the plugin screen nor a debug.log file in my wp-content after enabling the above two .
I also tried deleting using SQL like below :
global $wpdb;
$wpdb->query("DELETE FROM wp_posts WHERE post_type='book'");
$wpdb->query("DELETE from wp_postmeta WHERE post_id NOT IN( SELECT id FROM wp_posts");
Even this gives me Deletion Failed Error . After , deactivation I see the custom posts with the post_type in my database and I have no clue why this is not working .
Share Improve this question edited Jun 29, 2019 at 13:16 Knownow asked Jun 29, 2019 at 11:00 KnownowKnownow 1216 bronze badges 1- The problem was if(!defined(WP_UNINSTALL_PLUGIN)) , I forgot the quotes around the constant . I still do not understand how do I debug in WP while coding a plugin . In plain PHP I do print_r but here it gives me an error header already sent . – Knownow Commented Jun 29, 2019 at 17:37
2 Answers
Reset to default 0try with this :
$args = array (
'post_type' => 'book',
'nopaging' => true
);
$myquery = new WP_Query ($args);
while ($myquery->have_posts ()) {
$myquery->the_post ();
$id = get_the_ID ();
wp_delete_post ($id, true);
}
wp_reset_postdata ();
let me know if this works for you!
perhaps, after deactivating the plugin, your post_type is already undefined
try to delete through wpdb sql request
<?php
if (!defined("WP_UNINSTALL_PLUGIN"))
exit();
global $wpdb;
$post_type = 'book';
$result = $wpdb-> query (
$wpdb-> prepare ("
DELETE posts, pt, pm
FROM ". $wpdb-> prefix." Posts
LEFT JOIN ". $wpdb-> prefix." Term_relationships pt ON pt.object_id = posts.ID
LEFT JOIN ". $wpdb-> prefix." Postmeta pm ON pm.post_id = posts.ID
WHERE posts.post_type =% s
",
$post_type
)
);
本文标签: plugin developmentCustom Post not Deleting on Uninstall
版权声明:本文标题:plugin development - Custom Post not Deleting on Uninstall 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745359287a2655221.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论