• 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

    (@keesiemeijer)

    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/

    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.