wp_untrash_post

Restore a post or page from the Trash.

Description

(WP_Post|false) wp_untrash_post( (int) $post_id = 0 ); 

Returns (WP_Post|false)

WP_Post object. False on failure.

Parameters (1)

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

Usage

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

Defined (1)

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

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