bbp_update_forum_last_topic_id

Update the forum last topic id.

Description

bbp_update_forum_last_topic_id( (int) $forum_id = 0, (int) $topic_id = 0 ); 

Parameters (2)

0. $forum_id — Optional. (int)
Forum id
1. $topic_id — Optional. (int)
Topic id

Usage

  1. if ( !function_exists( 'bbp_update_forum_last_topic_id' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/forums/functions.php'; 
  3.  
  4. // Optional. Forum id 
  5. $forum_id = -1; 
  6.  
  7. // Optional. Topic id 
  8. $topic_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bbp_update_forum_last_topic_id($forum_id, $topic_id); 
  12.  

Defined (1)

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

/includes/forums/functions.php  
  1. function bbp_update_forum_last_topic_id( $forum_id = 0, $topic_id = 0 ) { 
  2. $forum_id = bbp_get_forum_id( $forum_id ); 
  3.  
  4. // Define local variable(s) 
  5. $children_last_topic = 0; 
  6.  
  7. // Do some calculation if not manually set 
  8. if ( empty( $topic_id ) ) { 
  9.  
  10. // Loop through children and add together forum reply counts 
  11. $children = bbp_forum_query_subforum_ids( $forum_id ); 
  12. if ( !empty( $children ) ) { 
  13. foreach ( (array) $children as $child ) { 
  14. $children_last_topic = bbp_update_forum_last_topic_id( $child ); // Recursive 
  15.  
  16. // Setup recent topic query vars 
  17. $post_vars = array( 
  18. 'post_parent' => $forum_id,  
  19. 'post_type' => bbp_get_topic_post_type(),  
  20. 'meta_key' => '_bbp_last_active_time',  
  21. 'orderby' => 'meta_value',  
  22. 'numberposts' => 1 
  23. ); 
  24.  
  25. // Get the most recent topic in this forum_id 
  26. $recent_topic = get_posts( $post_vars ); 
  27. if ( !empty( $recent_topic ) ) { 
  28. $topic_id = $recent_topic[0]->ID; 
  29.  
  30. // Cast as integer in case of empty or string 
  31. $topic_id = (int) $topic_id; 
  32. $children_last_topic = (int) $children_last_topic; 
  33.  
  34. // If child forums have higher id, use that instead 
  35. if ( !empty( $children ) && ( $children_last_topic > $topic_id ) ) 
  36. $topic_id = $children_last_topic; 
  37.  
  38. // Update the last public topic ID 
  39. if ( bbp_is_topic_published( $topic_id ) ) 
  40. update_post_meta( $forum_id, '_bbp_last_topic_id', $topic_id ); 
  41.  
  42. return (int) apply_filters( 'bbp_update_forum_last_topic_id', $topic_id, $forum_id );