WordPress at a glance

wp_delete_auto_drafts() WP 3.4.0

Delete auto-drafts for new posts that are more than 7 days old.

This function gets all auto-drafts that are older than 7 days and deletes them completely with wp_delete_post().

This function is called every day by WordPress cron with wp_schedule_event().

// file wp-admin/post-new.php:66
if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) {
	wp_schedule_event( time(), 'daily', 'wp_scheduled_auto_draft_delete' );

// file wp-includes/default-filters.php:325
add_action( 'wp_scheduled_auto_draft_delete', 'wp_delete_auto_drafts' );
Works based on: wp_delete_post()

No Hooks.


null. Nothing.




#1 Delete all auto-drafs

The function doesn't have any parameters, so there's only one way to use it:



  • Global. wpdb. $wpdb WordPress database abstraction object.


Since 3.4.0 Introduced.

Code of wp_delete_auto_drafts() WP 5.7.2

function wp_delete_auto_drafts() {
	global $wpdb;

	// Cleanup old auto-drafts more than 7 days old.
	$old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" );
	foreach ( (array) $old_posts as $delete ) {
		// Force delete.
		wp_delete_post( $delete, true );

From tag: cron (schedule)

More from category: Revisions

No comments
    Log In