wp_delete_auto_drafts()

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


Source

File: wp-includes/post.php. View all references

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 );
	}
}


Top ↑

Changelog

Changelog
Version Description
3.4.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by petruciucur

    Delete now without waiting 7 days

    function wp_delete_autodraft() {
        global $wpdb;
     
        // Delete auto-drafts.
        $old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft'" );
        foreach ( (array) $old_posts as $delete ) {
            // Force delete.
            wp_delete_post( $delete, true );
        }
    }
    add_action( 'init', 'wp_delete_autodraft' );

You must log in before being able to contribute a note or feedback.