bbp_delete_forum_topics

Delete all topics (and their replies) for a specific forum ID.

Description

bbp_delete_forum_topics( (int) $forum_id = 0 ); 

Parameters (1)

0. $forum_id — Optional. (int)
The forum id.

Usage

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

Defined (1)

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

/includes/forums/functions.php  
  1. function bbp_delete_forum_topics( $forum_id = 0 ) { 
  2.  
  3. // Validate forum ID 
  4. $forum_id = bbp_get_forum_id( $forum_id ); 
  5. if ( empty( $forum_id ) ) 
  6. return; 
  7.  
  8. // Forum is being permanently deleted, so its content has go too 
  9. // Note that we get all post statuses here 
  10. $topics = new WP_Query( array( 
  11. 'suppress_filters' => true,  
  12. 'post_type' => bbp_get_topic_post_type(),  
  13. 'post_parent' => $forum_id,  
  14. 'post_status' => array_keys( get_post_stati() ),  
  15. 'posts_per_page' => -1,  
  16. 'nopaging' => true,  
  17. 'fields' => 'id=>parent' 
  18. ) ); 
  19.  
  20. // Loop through and delete child topics. Topic replies will get deleted by 
  21. // the bbp_delete_topic() action. 
  22. if ( !empty( $topics->posts ) ) { 
  23. foreach ( $topics->posts as $topic ) { 
  24. wp_delete_post( $topic->ID, true ); 
  25.  
  26. // Reset the $post global 
  27. wp_reset_postdata(); 
  28.  
  29. // Cleanup 
  30. unset( $topics );