_bbp_has_replies_where

Used by bbp_has_replies() to add the lead topic post to the posts loop.

Description

(string) _bbp_has_replies_where( (string) $where = '', (bool) $query = false ); 

This function filters the post_where of the WP_Query, and changes the query to include both the topic AND its children in the same loop.

Returns (string)

Parameters (2)

0. $where — Optional. (string) => ''
The where.
1. $query — Optional. (bool) => false
The query.

Usage

  1. if ( !function_exists( '_bbp_has_replies_where' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/replies/functions.php'; 
  3.  
  4. // The where. 
  5. $where = ''; 
  6.  
  7. // The query. 
  8. $query = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = _bbp_has_replies_where($where, $query); 
  12.  

Defined (1)

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

/includes/replies/functions.php  
  1. function _bbp_has_replies_where( $where = '', $query = false ) { 
  2.  
  3. /** Bail ******************************************************************/ 
  4.  
  5. // Bail if the sky is falling 
  6. if ( empty( $where ) || empty( $query ) ) { 
  7. return $where; 
  8.  
  9. // Bail if no post_parent to replace 
  10. if ( ! is_numeric( $query->get( 'post_parent' ) ) ) { 
  11. return $where; 
  12.  
  13. // Bail if not a topic and reply query 
  14. if ( array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) !== $query->get( 'post_type' ) ) { 
  15. return $where; 
  16.  
  17. // Bail if including or excluding specific post ID's 
  18. if ( $query->get( 'post__not_in' ) || $query->get( 'post__in' ) ) { 
  19. return $where; 
  20.  
  21. /** Proceed ***************************************************************/ 
  22.  
  23. global $wpdb; 
  24.  
  25. // Table name for posts 
  26. $table_name = $wpdb->prefix . 'posts'; 
  27.  
  28. // Get the topic ID from the post_parent, set in bbp_has_replies() 
  29. $topic_id = bbp_get_topic_id( $query->get( 'post_parent' ) ); 
  30.  
  31. // The texts to search for 
  32. $search = array( 
  33. "FROM {$table_name} " ,  
  34. "WHERE 1=1 AND {$table_name}.post_parent = {$topic_id}" 
  35. ); 
  36.  
  37. // The texts to replace them with 
  38. $replace = array( 
  39. $search[0] . "FORCE INDEX (PRIMARY, post_parent) " ,  
  40. "WHERE 1=1 AND ({$table_name}.ID = {$topic_id} OR {$table_name}.post_parent = {$topic_id})" 
  41. ); 
  42.  
  43. // Try to replace the search text with the replacement 
  44. $new_where = str_replace( $search, $replace, $where ); 
  45. if ( ! empty( $new_where ) ) { 
  46. $where = $new_where; 
  47.  
  48. return $where;