wp_trash_post_comments

Moves comments for a post to the trash.

Description

(mixed|void) wp_trash_post_comments( (null) $post = null ); 

Returns (mixed|void)

False on failure.

Parameters (1)

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

Usage

  1. if ( !function_exists( 'wp_trash_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_trash_post_comments($post); 
  9.  

Defined (1)

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

/wp-includes/post.php  
  1. function wp_trash_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. /** 
  11. * Fires before comments are sent to the trash. 
  12. * @since 2.9.0 
  13. * @param int $post_id Post ID. 
  14. */ 
  15.  
  16. $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id) ); 
  17. if ( empty($comments) ) 
  18. return; 
  19.  
  20. // Cache current status for each comment. 
  21. $statuses = array(); 
  22. foreach ( $comments as $comment ) 
  23. $statuses[$comment->comment_ID] = $comment->comment_approved; 
  24. add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses); 
  25.  
  26. // Set status for all comments to post-trashed. 
  27. $result = $wpdb->update($wpdb->comments, array('comment_approved' => 'post-trashed'), array('comment_post_ID' => $post_id)); 
  28.  
  29. clean_comment_cache( array_keys($statuses) ); 
  30.  
  31. /** 
  32. * Fires after comments are sent to the trash. 
  33. * @since 2.9.0 
  34. * @param int $post_id Post ID. 
  35. * @param array $statuses Array of comment statuses. 
  36. */ 
  37. do_action( 'trashed_post_comments', $post_id, $statuses ); 
  38.  
  39. return $result;