ZACK BRADY

Programmatically Bulk Delete WordPress Posts and Terms

Tweet

I’m gonna just leave these little bits of code here …

So one of my current clients required a large amount of information to be transferred from another CMS into WordPress (I’m talking the equivalent of ten thousand posts). As one may assume, this was not without some trial and error due to fun things like text not being UTF-8, spelling mistakes on my part, and the likes.

So what happens when you have 10,000 worthless posts? I don’t think anyone can delete each one manual in non-geological time. While there are a few plug-ins out there I always like to use code when I can. Unfortunately this is one of the hardest snippets of code to find. So to help the world I’ll just leave these two snippets here. (To see the thread where I found them go HERE.

Remove 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.?>



Remove Taxonomy 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 );
}
}
?>


Web Design and Development, Wordpress
wplogoblue-stacked-rgb