bbp_update_forum_reply_count

Adjust the total reply count of a forum.

Description

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

Returns (int)

Forum reply 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_reply_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_reply_count($forum_id); 
  7.  

Defined (1)

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

/includes/forums/functions.php  
  1. function bbp_update_forum_reply_count( $forum_id = 0 ) { 
  2. global $wpdb; 
  3.  
  4. $forum_id = bbp_get_forum_id( $forum_id ); 
  5. $children_reply_count = 0; 
  6.  
  7. // Loop through children and add together forum reply counts 
  8. $children = bbp_forum_query_subforum_ids( $forum_id ); 
  9. if ( !empty( $children ) ) { 
  10. foreach ( (array) $children as $child ) { 
  11. $children_reply_count += bbp_update_forum_reply_count( $child ); 
  12.  
  13. // Don't count replies if the forum is a category 
  14. $topic_ids = bbp_forum_query_topic_ids( $forum_id ); 
  15. if ( !empty( $topic_ids ) ) { 
  16. $topic_ids = implode( ', ', wp_parse_id_list( $topic_ids ) ); 
  17. $reply_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type() ) ); 
  18. } else { 
  19. $reply_count = 0; 
  20.  
  21. // Calculate total replies in this forum 
  22. $total_replies = (int) $reply_count + $children_reply_count; 
  23.  
  24. // Update the count 
  25. update_post_meta( $forum_id, '_bbp_reply_count', (int) $reply_count ); 
  26. update_post_meta( $forum_id, '_bbp_total_reply_count', (int) $total_replies ); 
  27.  
  28. return (int) apply_filters( 'bbp_update_forum_reply_count', (int) $total_replies, $forum_id );