wp_delete_auto_drafts()
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' );
Uses: wp_delete_post()
No Hooks.
Returns
null. Nothing (null).
Usage
wp_delete_auto_drafts();
Examples
#1 Delete all auto-drafs
The function doesn't have any parameters, so there's only one way to use it:
wp_delete_auto_drafts();
Notes
- Global. wpdb.
$wpdbWordPress database abstraction object.
Changelog
| Since 3.4.0 | Introduced. |
wp_delete_auto_drafts() wp delete auto drafts code WP 6.9.1
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 );
}
}