• Ambyomoron

    (@josiah-s-carberry)


    I have a number of rows in the database in table wp_posts whose post_type is not valid. In general, they were created by plugins that did not clean up after themselves when they were deleted. I would like to clean up these useless rows and any related data. The plugins that created the post types are long since gone, so I cannot consult them for any advice.

    I am comfortable with doing deleting rows using phpmyadmin, but am also aware of the function wp_delete_posts(). In this context I have a few questions:

    1) For a given post type, is there a row in some table that defines it, or is a post type simply a value in the post_types column of the wp_posts table? In other words, if I delete all the rows of wp_posts where post_type = ‘something’, will that effectively remove the post type from the database?

    2) I am aware that some rows in wp_postmeta might be related to the rows in wp_posts and I will want to delete them, too. But, is there a programatic way of identifying relations that might exist in other tables? Or do I just need to base my judgement on the table names?

    3) It seems that the function wp_delete_posts() would to the job for me, but I am ensure how to use it. Should I add a snippet of code, such as the following:

    $allposts= get_posts( array('post_type'=>'type-to-delete','numberposts'=>-1) );
    foreach ($allposts as $eachpost) {
      wp_delete_post( $eachpost->ID, true );
    }

    to the themes functions.php, and then delete it once the job is done? Or is there a better way to do this?

    Many thanks for advice on achieving this goal.

Viewing 1 replies (of 1 total)
  • Hi @josiah-s-carberry

    You can delete all post via $wpdb

    
    DELETE FROM wp_posts WHERE post_type='post_type';
    DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT id FROM wp_posts);
    DELETE FROM wp_term_relationships WHERE object_id NOT IN (SELECT id FROM wp_posts)

    or use this query replace it with {{your CPT}} with your Custom Post Type

    
    DELETE a,b,c
        FROM wp_posts a
        LEFT JOIN wp_term_relationships b
            ON (a.ID = b.object_id)
        LEFT JOIN wp_postmeta c
            ON (a.ID = c.post_id)
        WHERE a.post_type = '{{your CPT}}';

    Try like this
    Thanks

Viewing 1 replies (of 1 total)
  • The topic ‘deleting obsolete post types’ is closed to new replies.