Title: Specify Date Range?
Last modified: August 26, 2020

---

# Specify Date Range?

 *  [viablethought](https://wordpress.org/support/users/viablethought/)
 * (@viablethought)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/specify-date-range-2/)
 * Hello –
 * We have a plugin that stores log data in regard to WP, plugin, and theme updates
   to send reports to clients, is there a way to specify a date range in which to
   target old CPT’s to be removed? e.g Remove everything previous to 30 days ago?
   Or “remove everything from 1/1/2019 – 12/31/2019”
 * Sites that we’ve managed for a couple of years have a ton of old junk in the 
   WP Posts and WP Posts Meta tables that we want to dump, but want to keep the 
   past 30 days worth of data for reporting.
 * Thanks

Viewing 1 replies (of 1 total)

 *  Plugin Author [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/specify-date-range-2/#post-13322076)
 * Hi Jason Ryan
 * You can’t filter the queries this plugin does to get the posts to delete, but
   you could use the WordPress action `pre_delete_post` to filter out the posts 
   by date.
 * [https://developer.wordpress.org/reference/hooks/pre_delete_post/](https://developer.wordpress.org/reference/hooks/pre_delete_post/)
 * This example shows you how to only delete posts older than 30 days. It only has
   this restriction in the admin page of this plugin.
 *     ```
       add_action( 'load-tools_page_custom-post-type-cleanup', 'cpt_cleanup_admin_page' );
   
       function cpt_cleanup_admin_page() {
       	// Plugin admin page is loaded.
   
       	$request = cptc_get_request( 'check_referer' );
       	if ( 'delete' !== $request ) {
       		// Not a delete request.
       		return;
       	}
   
       	// Filter posts by date
       	add_filter( 'pre_delete_post', 'filter_delete_unused_post_type_by_date', 10, 2 );
       }
   
       function filter_delete_unused_post_type_by_date( $delete, $post ) {
       	if ( strtotime( $post->post_date ) > strtotime( '-30 days' ) ) {
       		// Don't delete posts from the last 30 days.
       		return false;
       	}
   
       	return $delete;
       }
       ```
   
 * Please make a database backup before using this code.

Viewing 1 replies (of 1 total)

The topic ‘Specify Date Range?’ is closed to new replies.

 * ![](https://ps.w.org/custom-post-type-cleanup/assets/icon.svg?rev=1716282)
 * [Custom Post Type Cleanup](https://wordpress.org/plugins/custom-post-type-cleanup/)
 * [Support Threads](https://wordpress.org/support/plugin/custom-post-type-cleanup/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-post-type-cleanup/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-post-type-cleanup/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-post-type-cleanup/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [5 years, 9 months ago](https://wordpress.org/support/topic/specify-date-range-2/#post-13322076)
 * Status: not resolved