wp_scheduled_delete

Permanently delete comments or posts of any type that have held a status of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.

Description

wp_scheduled_delete(); 

The default value of EMPTY_TRASH_DAYS is 30 (days).


Usage

  1. if ( !function_exists( 'wp_scheduled_delete' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_scheduled_delete(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function wp_scheduled_delete() { 
  2. global $wpdb; 
  3.  
  4. $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ); 
  5.  
  6. $posts_to_delete = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A); 
  7.  
  8. foreach ( (array) $posts_to_delete as $post ) { 
  9. $post_id = (int) $post['post_id']; 
  10. if ( !$post_id ) 
  11. continue; 
  12.  
  13. $del_post = get_post($post_id); 
  14.  
  15. if ( !$del_post || 'trash' != $del_post->post_status ) { 
  16. delete_post_meta($post_id, '_wp_trash_meta_status'); 
  17. delete_post_meta($post_id, '_wp_trash_meta_time'); 
  18. } else { 
  19. wp_delete_post($post_id); 
  20.  
  21. $comments_to_delete = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < '%d'", $delete_timestamp), ARRAY_A); 
  22.  
  23. foreach ( (array) $comments_to_delete as $comment ) { 
  24. $comment_id = (int) $comment['comment_id']; 
  25. if ( !$comment_id ) 
  26. continue; 
  27.  
  28. $del_comment = get_comment($comment_id); 
  29.  
  30. if ( !$del_comment || 'trash' != $del_comment->comment_approved ) { 
  31. delete_comment_meta($comment_id, '_wp_trash_meta_time'); 
  32. delete_comment_meta($comment_id, '_wp_trash_meta_status'); 
  33. } else { 
  34. wp_delete_comment( $del_comment );