Search code examples
mysqlwordpresscustom-post-type

Deleting Custom Post type data using mySQL command


I have around 2000 posts under a custom post type storelocations. Deleting them from WordPress admin takes a lot of time.

  1. How to delete all the posts from this custom post type from database using mySQL command. Here is my approach:

    DELETE FROM 'wp_posts' WHERE 'post_type' = 'storelocations'

    But I guess the post also have some data in wp_postmeta and some other tables maybe. And I guess I should delete data from there too.

    So, may I know which tables have the data for a post. And what command I should run to clear them.

  2. Another doubt that I have is: why deleting the posts from WordPress admin takes so much time compared to deleting the posts from mySQL. Isn't WordPress doing the same thing as running few SQL commands, or there is much going on?


Solution

    1. you can first delete everything in wp_postmeta by running

    DELETE FROM wp_postmeta where post_id IN (SELECT ID from wp_posts where post_type = 'storelocations' );

    optionally you can delete all the comments too

    DELETE FROM wp_comments where comment_post_ID IN (SELECT ID from wp_posts where post_type = 'storelocations' );

    and finally, you can delete all the posts

    DELETE from wp_posts where post_type = 'storelocations'

    1. since webpages like WordPress don't run the SQL-statements directly, which means there's apache which loads your request, then there's PHP which parses the dynamic content and finally, there's SQL executed, this can take some time.