bbp_delete_topic

Called before deleting a topic.

Description

bbp_delete_topic( (int) $topic_id = 0 ); 

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

Parameters (1)

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

Usage

  1. if ( !function_exists( 'bbp_delete_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_delete_topic($topic_id); 
  9.  

Defined (1)

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

/includes/topics/functions.php  
  1. function bbp_delete_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_delete_topic', $topic_id ); 
  10.  
  11. // Topic is being permanently deleted, so its replies gotta go too 
  12. // Note that we get all post statuses here 
  13. $replies = new WP_Query( array( 
  14. 'suppress_filters' => true,  
  15. 'post_type' => bbp_get_reply_post_type(),  
  16. 'post_status' => array_keys( get_post_stati() ),  
  17. 'post_parent' => $topic_id,  
  18. 'posts_per_page' => -1,  
  19. 'nopaging' => true,  
  20. 'fields' => 'id=>parent' 
  21. ) ); 
  22.  
  23. // Loop through and delete child replies 
  24. if ( ! empty( $replies->posts ) ) { 
  25. foreach ( $replies->posts as $reply ) { 
  26. wp_delete_post( $reply->ID, true ); 
  27.  
  28. // Reset the $post global 
  29. wp_reset_postdata(); 
  30.  
  31. // Cleanup 
  32. unset( $replies );