bbp_update_topic_voice_count

Adjust the total voice count of a topic.

Description

(int) bbp_update_topic_voice_count( (int) $topic_id = 0 ); 

Returns (int)

Voice count

Parameters (1)

0. $topic_id — Optional. (int)
Topic id to update

Usage

  1. if ( !function_exists( 'bbp_update_topic_voice_count' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/topics/functions.php'; 
  3.  
  4. // Optional. Topic id to update 
  5. $topic_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bbp_update_topic_voice_count($topic_id); 
  9.  

Defined (1)

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

/includes/topics/functions.php  
  1. function bbp_update_topic_voice_count( $topic_id = 0 ) { 
  2. global $wpdb; 
  3.  
  4. // If it's a reply, then get the parent (topic id) 
  5. if ( bbp_is_reply( $topic_id ) ) { 
  6. $topic_id = bbp_get_reply_topic_id( $topic_id ); 
  7. } elseif ( bbp_is_topic( $topic_id ) ) { 
  8. $topic_id = bbp_get_topic_id( $topic_id ); 
  9. } else { 
  10. return; 
  11.  
  12. // Query the DB to get voices in this topic 
  13. $voices = $wpdb->get_col( $wpdb->prepare( "SELECT COUNT( DISTINCT post_author ) FROM {$wpdb->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' ) OR ( ID = %d AND post_type = '%s' );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ) ); 
  14.  
  15. // If there's an error, make sure we have at least have 1 voice 
  16. $voices = ( empty( $voices ) || is_wp_error( $voices ) ) ? 1 : $voices[0]; 
  17.  
  18. // Update the voice count for this topic id 
  19. update_post_meta( $topic_id, '_bbp_voice_count', (int) $voices ); 
  20.  
  21. return apply_filters( 'bbp_update_topic_voice_count', (int) $voices, $topic_id );