WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How can I bulk delete all posts in a custom post type? (6 posts)

  1. Nick5a1
    Member
    Posted 2 years ago #

    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

  2. Nick5a1
    Member
    Posted 2 years ago #

    Anyone?

  3. Ervald
    Member
    Posted 2 years ago #

    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 );
         }
     }
    ?>
  4. Nick5a1
    Member
    Posted 2 years ago #

    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.

  5. Ervald
    Member
    Posted 2 years ago #

    No problem Nick,

    You can mark this post as Solved now I guess.

  6. scott74
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic