bbp_trash_topic

Called before trashing a topic.

Description

bbp_trash_topic( (int) $topic_id = 0 ); 

This function is supplemental to the actual topic being trashed which is handled by WordPress core API functions. It is used to clean up after a topic that is being trashed.

Parameters (1)

0. $topic_id — Optional. (int)
The topic id.

Usage

  1. if ( !function_exists( 'bbp_trash_topic' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/topics/functions.php'; 
  3.  
  4. // The topic id. 
  5. $topic_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bbp_trash_topic($topic_id); 
  9.  

Defined (1)

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

/includes/topics/functions.php  
  1. function bbp_trash_topic( $topic_id = 0 ) { 
  2.  
  3. // Validate topic ID 
  4. $topic_id = bbp_get_topic_id( $topic_id ); 
  5.  
  6. if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) ) 
  7. return false; 
  8.  
  9. do_action( 'bbp_trash_topic', $topic_id ); 
  10.  
  11. // Topic is being trashed, so its replies are trashed too 
  12. $replies = new WP_Query( array( 
  13. 'suppress_filters' => true,  
  14. 'post_type' => bbp_get_reply_post_type(),  
  15. 'post_status' => bbp_get_public_status_id(),  
  16. 'post_parent' => $topic_id,  
  17. 'posts_per_page' => -1,  
  18. 'nopaging' => true,  
  19. 'fields' => 'id=>parent' 
  20. ) ); 
  21.  
  22. if ( !empty( $replies->posts ) ) { 
  23.  
  24. // Prevent debug notices 
  25. $pre_trashed_replies = array(); 
  26.  
  27. // Loop through replies, trash them, and add them to array 
  28. foreach ( $replies->posts as $reply ) { 
  29. wp_trash_post( $reply->ID ); 
  30. $pre_trashed_replies[] = $reply->ID; 
  31.  
  32. // Set a post_meta entry of the replies that were trashed by this action. 
  33. // This is so we can possibly untrash them, without untrashing replies 
  34. // that were purposefully trashed before. 
  35. update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies ); 
  36.  
  37. // Reset the $post global 
  38. wp_reset_postdata(); 
  39.  
  40. // Cleanup 
  41. unset( $replies );