• I have a site that has thousands and thousands of posts dating back to around 2004. I’d really like to get rid of a lot of that old content and clean up our database.

    What is the best method to bulk delete posts by age and also clean up all of the related meta data? Is there a plugin, or mysql query, or something else?

    Any help is appreciated!

Viewing 1 replies (of 1 total)
  • Put this in your functions.php file, make sure to change 2004 in the 22nd line to set the max date you want to list its posts, for 2004, it should list all posts published before or in 2004:

    function delete_old_content($max_date) {
    	require_once( ABSPATH. "wp-blog-header.php" );
    	query_posts('showposts=-1');
    	if(have_posts()) {
    		while(have_posts()) : the_post();
    			$ids = get_the_ID(). ',';
    			$list = array( $ids );
    			foreach ($list as $items) {
    				$date = get_the_date("Y");
    				if($date <= $max_date) {
    					echo "<strong>".get_the_title(). "</strong> => ".get_the_date("Y"). "<br />";
    				}
    			}
    		endwhile;
    		wp_reset_query();
    	}
    }
    
    function delete_old_content_shortcode() {
    	if ( current_user_can( 'manage_options' ) ) {
    		ob_start();
    		delete_old_content("2004");
    		return ob_get_clean();
    	}
    }
    add_shortcode('delete-old-content', 'delete_old_content_shortcode');

    After that, add this shortcode [delete-old-content] in a new post/page, save and visit that post/page, and tell me if it is listing the desired posts you want to delete.

    Please note that yet we are not deleting anything, just making sure we selected the right targets.

Viewing 1 replies (of 1 total)
  • The topic ‘Delete posts and related meta older than X years?’ is closed to new replies.