WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How can I bulk delete all posts in a custom post type?

[Resolved] How can I bulk delete all posts in a custom post type?

  • Hi,

    I realise there are a few plugins out there for bulk deleting posts, but none that I have come across work with custom post types. I also can not access the custom post type page in the admin area as it just times out due to too many custom post types (over 15k). I need to delete all these custom posts then do a different import. It was previously advised to me and I tried deleting the following tables in my mySQL database:

    wp_posts
    wp_postmeta
    wp_terms
    wp_term_relationships
    wp_term_taxonomy

    Although this deleted all the custom posts I could no longer post a new post in that custom post type, and consequently could not re-import. I since restored to a previous version of my mySQL database and the tables are intact again.

    Can anyone please suggest how I can delete all the posts of the custom post type without destroying all the extensive customisation that has gone into the custom post type?

    Many thanks.
    Nick

Viewing 5 replies - 1 through 5 (of 5 total)
  • Anyone?

    Place this to a page:

    // For Custom posts

    <?php
    // Get 50 custom post types pages, set the number higher if is not slow.
    
    $mycustomposts = get_pages( array( 'post_type' => 'name-of-post-type', 'number' => 50) );
       foreach( $mycustomposts as $mypost ) {
         // Delete's each post.
         wp_delete_post( $mypost->ID, true);
        // Set to False if you want to send them to Trash.
       }
    // 50 custom post types are being deleted everytime you refresh the page.?>

    // For terms

    <?php
    $taxonomy = 'my_taxonomy';
    
    $terms = get_terms($taxonomy);
     $count = count($terms);
     if ( $count > 0 ){
    
         foreach ( $terms as $term ) {
            wp_delete_term( $term->term_id, $taxonomy );
         }
     }
    ?>

    Thanks Ervald this worked perfectly! I was able to change the number to 5,000 and just refresh it 3 times, took a couple minutes each time.

    No problem Nick,

    You can mark this post as Solved now I guess.

    Where exactly to you place this code.. I need to delete a ton of post from a custom post type… Also…Do you need to change anything in the code above..

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] How can I bulk delete all posts in a custom post type?’ is closed to new replies.
Skip to toolbar