bbp_update_forum_topic_count

Adjust the total topic count of a forum.

Description

(int) bbp_update_forum_topic_count( (int) $forum_id = 0 ); 

Returns (int)

Forum topic count

Parameters (1)

0. $forum_id — Optional. (int)
Forum id or topic id. It is checked whether it is a topic or a forum. If it's a topic, its parent, i.e. the forum is automatically retrieved.

Usage

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

Defined (1)

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

/includes/forums/functions.php  
  1. function bbp_update_forum_topic_count( $forum_id = 0 ) { 
  2. $forum_id = bbp_get_forum_id( $forum_id ); 
  3. $children_topic_count = 0; 
  4.  
  5. // Loop through subforums and add together forum topic counts 
  6. $children = bbp_forum_query_subforum_ids( $forum_id ); 
  7. if ( !empty( $children ) ) { 
  8. foreach ( (array) $children as $child ) { 
  9. $children_topic_count += bbp_update_forum_topic_count( $child ); // Recursive 
  10.  
  11. // Get total topics for this forum 
  12. $topics = (int) count( bbp_forum_query_topic_ids( $forum_id ) ); 
  13.  
  14. // Calculate total topics in this forum 
  15. $total_topics = $topics + $children_topic_count; 
  16.  
  17. // Update the count 
  18. update_post_meta( $forum_id, '_bbp_topic_count', (int) $topics ); 
  19. update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topics ); 
  20.  
  21. return (int) apply_filters( 'bbp_update_forum_topic_count', (int) $total_topics, $forum_id );