wp_trash_post

Move a post or page to the Trash.

Description

(false|array|WP_Post|null) wp_trash_post( (int) $post_id = 0 ); 

If trash is disabled, the post or page is permanently deleted.

Returns (false|array|WP_Post|null)

Post data array, otherwise false.

Parameters (1)

0. $post_id — Optional. (int)
Post ID. Default is ID of the global $post if EMPTY_TRASH_DAYS equals true.

Usage

  1. if ( !function_exists( 'wp_trash_post' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Optional. Post ID. Default is ID of the global $post 
  5. // if EMPTY_TRASH_DAYS equals true. 
  6. $post_id = -1; 
  7.  
  8. // NOTICE! Understand what this does before running. 
  9. $result = wp_trash_post($post_id); 
  10.  

Defined (1)

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

/wp-includes/post.php  
  1. function wp_trash_post( $post_id = 0 ) { 
  2. if ( !EMPTY_TRASH_DAYS
  3. return wp_delete_post($post_id, true); 
  4.  
  5. if ( !$post = get_post($post_id, ARRAY_A) ) 
  6. return $post; 
  7.  
  8. if ( $post['post_status'] == 'trash' ) 
  9. return false; 
  10.  
  11. /** 
  12. * Fires before a post is sent to the trash. 
  13. * @since 3.3.0 
  14. * @param int $post_id Post ID. 
  15. */ 
  16. do_action( 'wp_trash_post', $post_id ); 
  17.  
  18. add_post_meta($post_id, '_wp_trash_meta_status', $post['post_status']); 
  19. add_post_meta($post_id, '_wp_trash_meta_time', time()); 
  20.  
  21. $post['post_status'] = 'trash'; 
  22. wp_insert_post( wp_slash( $post ) ); 
  23.  
  24.  
  25. /** 
  26. * Fires after a post is sent to the trash. 
  27. * @since 2.9.0 
  28. * @param int $post_id Post ID. 
  29. */ 
  30. do_action( 'trashed_post', $post_id ); 
  31.  
  32. return $post;