PHP WordPress Delete All Posts with thumbnail image permanently

Delete all image, meta data and post permanently.


# delete all posts
$args = array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'post_status' => 'any'
);
$posts = get_posts($args);
foreach ($posts as $post) {
    # delete post thumbnail
    $thumbnail_id = get_post_thumbnail_id($post->ID);
    if ($thumbnail_id) wp_delete_attachment($thumbnail_id, true);
    wp_delete_post($post->ID, true);
}

Leave a Reply

Your email address will not be published.