wp_untrash_post_comments

Restore comments for a post from the trash.

Description

(true|void) wp_untrash_post_comments( (null) $post = null ); 

Returns (true|void)

Parameters (1)

0. $post — Optional. (null) => null
Post ID or post object. Defaults to global $post.

Usage

  1. if ( !function_exists( 'wp_untrash_post_comments' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Optional. Post ID or post object. Defaults to global $post. 
  5. $post = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_untrash_post_comments($post); 
  9.  

Defined (1)

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

/wp-includes/post.php  
  1. function wp_untrash_post_comments( $post = null ) { 
  2. global $wpdb; 
  3.  
  4. $post = get_post($post); 
  5. if ( empty($post) ) 
  6. return; 
  7.  
  8. $post_id = $post->ID; 
  9.  
  10. $statuses = get_post_meta($post_id, '_wp_trash_meta_comments_status', true); 
  11.  
  12. if ( empty($statuses) ) 
  13. return true; 
  14.  
  15. /** 
  16. * Fires before comments are restored for a post from the trash. 
  17. * @since 2.9.0 
  18. * @param int $post_id Post ID. 
  19. */ 
  20.  
  21. // Restore each comment to its original status. 
  22. $group_by_status = array(); 
  23. foreach ( $statuses as $comment_id => $comment_status ) 
  24. $group_by_status[$comment_status][] = $comment_id; 
  25.  
  26. foreach ( $group_by_status as $status => $comments ) { 
  27. // Sanity check. This shouldn't happen. 
  28. if ( 'post-trashed' == $status ) { 
  29. $status = '0'; 
  30. $comments_in = implode( ', ', array_map( 'intval', $comments ) ); 
  31. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) ); 
  32.  
  33. clean_comment_cache( array_keys($statuses) ); 
  34.  
  35. delete_post_meta($post_id, '_wp_trash_meta_comments_status'); 
  36.  
  37. /** 
  38. * Fires after comments are restored for a post from the trash. 
  39. * @since 2.9.0 
  40. * @param int $post_id Post ID. 
  41. */