0

I have some categories on a site with over 350,000 active posts some of which have as many as 50,000 posts. The volume makes using a bulk delete plugins not viable because they all timeout and delete nothing. The best feedback I've gotten from the Wordpress support forum is to use WP-CLI but beyond that all they've done is give me a link to the WP-CLI website which contains no example specific to my needs.

I have never used CLI before beyond a failed attempt to run Media Cleaner which resulted in a timeout at 999 seconds. That was months ago so I forget how I tried to do that. Could someone please post a code snippet that could basically be copied and pasted into CLI with the only change I need to make being swapping the category ID or date/ID number of the oldest post I want to keep?

Again, I want to be able to delete all posts from a specific category or before a specific date/ID.

Alternatively, could someone post a SQL statement so that I can do this using MySQL without installing CLI? The SQL would be better because I could apply it to any site without installing let alone learning CLI.

1 Answers1

0

You don't have access to phpmyadmin?

You could avoid all of that just by deleting posts on load:

/**
 * REMOVE EVIL POSTS IF AND WHEN LOADED
 * add to functions.php
 * works on regular posts and simple categories, 
 * check codex for details if custom post types, custom taxonomies, etc.
 **/
add_action( 'template_redirect', 'delete_post_on_load' ) ;

function delete_post_on_load() {              

     global $post ;

    //has term works more consistently if all variables are supplied
     if ( has_term( array( 'Category to Go', 'Second Category to Go' ), 'category', $post->ID ) ) { 
        
        wp_delete_post( $post->ID, true ) ;
        wp_safe_redirect( site_url() ) ; //redirect to home instead of 404 on deletion
        exit() ;
        
     }

}
CK MacLeod
  • 932
  • 6
  • 10