bbp_trash_forum_topics

Trash all topics inside a forum.

Description

bbp_trash_forum_topics( (int) $forum_id = 0 ); 

Parameters (1)

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

Usage

  1. if ( !function_exists( 'bbp_trash_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_trash_forum_topics($forum_id); 
  9.  

Defined (1)

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

/includes/forums/functions.php  
  1. function bbp_trash_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. // Allowed post statuses to pre-trash 
  9. $post_stati = implode( ', ', array( 
  10. ) ); 
  11.  
  12. // Forum is being trashed, so its topics and replies are trashed too 
  13. $topics = new WP_Query( array( 
  14. 'suppress_filters' => true,  
  15. 'post_type' => bbp_get_topic_post_type(),  
  16. 'post_parent' => $forum_id,  
  17. 'post_status' => $post_stati,  
  18. 'posts_per_page' => -1,  
  19. 'nopaging' => true,  
  20. 'fields' => 'id=>parent' 
  21. ) ); 
  22.  
  23. // Loop through and trash child topics. Topic replies will get trashed by 
  24. // the bbp_trash_topic() action. 
  25. if ( !empty( $topics->posts ) ) { 
  26.  
  27. // Prevent debug notices 
  28. $pre_trashed_topics = array(); 
  29.  
  30. // Loop through topics, trash them, and add them to array 
  31. foreach ( $topics->posts as $topic ) { 
  32. wp_trash_post( $topic->ID, true ); 
  33. $pre_trashed_topics[] = $topic->ID; 
  34.  
  35. // Set a post_meta entry of the topics that were trashed by this action. 
  36. // This is so we can possibly untrash them, without untrashing topics 
  37. // that were purposefully trashed before. 
  38. update_post_meta( $forum_id, '_bbp_pre_trashed_topics', $pre_trashed_topics ); 
  39.  
  40. // Reset the $post global 
  41. wp_reset_postdata(); 
  42.  
  43. // Cleanup 
  44. unset( $topics );