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
Add a comment  | 

2 Answers 2

Reset to default 0

try 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