Jason Hoffmann
Jason Hoffmann

Reputation: 825

Removing Content with wp_delete_post in a Plugin with Custom Post Type

I set up a plugin that adds a custom post type and then brings in a bunch of dummy content with wp_insert_post on activation like so:

    register_activation_hook( __FILE__, array( $this, 'activate' ) );
public function activate( $network_wide ) {
        include 'dummycontent.php';
        foreach ($add_posts_array as $post){
        wp_insert_post( $post );
    };
} // end activate

I would like to remove this content when the plugin is deactivated so I set up this function:

    register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
   public function deactivate( $network_wide ) {
        include 'dummycontent.php';
    foreach($remove_posts_array as $array){
        $page_name = $array["post_title"];
        global $wpdb;
        $page_name_id = $wpdb->get_results("SELECT ID FROM " . $wpdb->base_prefix . "posts WHERE post_title = '". $page_name ."'");
        foreach($page_name_id as $page_name_id){
            $page_name_id = $page_name_id->ID;
            wp_delete_post( $page_name_id, true );
        };
    };      
} // end deactivate

It works just fine. Except because the custom post type is created with the same plugin that these two functions are run through, the post type is removed before the posts themselves can be through wp_delete_post. When I test these functions out without the custom post type posts are added upon activation and removed upon deactivation. So I know the problem is with the post type. Does anyone know how to work around this?

Upvotes: 0

Views: 7465

Answers (3)

MV.
MV.

Reputation: 944

Try something like this (YOURPOSTTYPE is the name of your post type):

function deactivate () {
  $args = array (
    'post_type' => 'YOURPOSTTYPE',
    'nopaging' => true
  );
  $query = new WP_Query ($args);
  while ($query->have_posts ()) {
    $query->the_post ();
    $id = get_the_ID ();
    wp_delete_post ($id, true);
  }
  wp_reset_postdata ();
}

It works in my plugin, it should works in your's. (This has been tested with WordPress 3.5.1).

Upvotes: 3

Asakkour Soufiane
Asakkour Soufiane

Reputation: 514

Try this Function

function deactivate () {             
        $args = array(
            'post_type'      => 'POST_TYPE', 
            'posts_per_page' => - 1
        );

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

Upvotes: 0

CodeAngry
CodeAngry

Reputation: 12995

wp_delete_post($ID, false) sends it to Trash. Only when you remove from Trash is a post really deleted. That's why it works with $force = true.

So it works as expected. First posts go to Trash, then they get actually deleted. Like Recycle Bin. Trace the post_status change to see when it hits the Trash if you want to do anything then. Otherwise wait for the delete.

Also delete content on uninstall and not on deactivate. Consider deactivating a plugin as pausing it and uninstalling it when you really want it gone.

Upvotes: 1

Related Questions