bbp_get_reply_position_raw

Get the position of a reply by querying the DB directly for the replies of a given topic.

Description

bbp_get_reply_position_raw( (int) $reply_id = 0, (int) $topic_id = 0 ); 

Parameters (2)

0. $reply_id — Optional. (int)
The reply id.
1. $topic_id — Optional. (int)
The topic id.

Usage

  1. if ( !function_exists( 'bbp_get_reply_position_raw' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/replies/functions.php'; 
  3.  
  4. // The reply id. 
  5. $reply_id = -1; 
  6.  
  7. // The topic id. 
  8. $topic_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bbp_get_reply_position_raw($reply_id, $topic_id); 
  12.  

Defined (1)

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

/includes/replies/functions.php  
  1. function bbp_get_reply_position_raw( $reply_id = 0, $topic_id = 0 ) { 
  2.  
  3. // Get required data 
  4. $reply_id = bbp_get_reply_id( $reply_id ); 
  5. $topic_id = !empty( $topic_id ) ? bbp_get_topic_id( $topic_id ) : bbp_get_reply_topic_id( $reply_id ); 
  6. $reply_position = 0; 
  7.  
  8. // If reply is actually the first post in a topic, return 0 
  9. if ( $reply_id !== $topic_id ) { 
  10.  
  11. // Make sure the topic has replies before running another query 
  12. $reply_count = bbp_get_topic_reply_count( $topic_id, false ); 
  13. if ( !empty( $reply_count ) ) { 
  14.  
  15. // Get reply id's 
  16. $topic_replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() ); 
  17. if ( !empty( $topic_replies ) ) { 
  18.  
  19. // Reverse replies array and search for current reply position 
  20. $topic_replies = array_reverse( $topic_replies ); 
  21. $reply_position = array_search( (string) $reply_id, $topic_replies ); 
  22.  
  23. // Bump the position to compensate for the lead topic post 
  24. $reply_position++; 
  25.  
  26. return (int) $reply_position;