BBoss_Global_Search_CPT

BuddyPress Global Search - search posts **************************************.

Defined (1)

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

/includes/plugins/search-cpt/class.BBoss_Global_Search_CPT.php  
  1. class BBoss_Global_Search_CPT extends BBoss_Global_Search_Type { 
  2. private $cpt_name; 
  3. private $search_type; 
  4.  
  5. /** 
  6. * A real constructor. Since we do want multiple copies of this class. 
  7. * The idea is to have one object for each searchable custom post type. 
  8. * @since 1.0.0 
  9. */ 
  10. public function __construct( $cpt_name, $search_type ) { 
  11. $this->cpt_name = $cpt_name; 
  12. $this->search_type =$search_type; 
  13.  
  14. public function sql( $search_term, $only_totalrow_count=false ) { 
  15.  
  16. global $wpdb; 
  17. $query_placeholder = array();  
  18.  
  19. $sql = " SELECT "; 
  20.  
  21. if( $only_totalrow_count ) { 
  22. $sql .= " COUNT( DISTINCT id ) "; 
  23. } else { 
  24. $sql .= " DISTINCT id , %s as type, post_title LIKE '%%%s%%' AS relevance, post_date as entry_date "; 
  25. $query_placeholder[] = $this->search_type; 
  26. $query_placeholder[] = $search_term; 
  27.  
  28. $sql .= " FROM  
  29. {$wpdb->prefix}posts  
  30. WHERE  
  31. 1=1  
  32. AND ( 
  33. (post_title LIKE '%%%s%%')  
  34. OR (post_content LIKE '%%%s%%') 
  35. )  
  36. AND post_type = %s  
  37. AND post_status = 'publish'  
  38. "; 
  39. $query_placeholder[] = $search_term; 
  40. $query_placeholder[] = $search_term; 
  41. $query_placeholder[] = $this->cpt_name; 
  42. $sql = $wpdb->prepare( $sql, $query_placeholder ); 
  43. return apply_filters(  
  44. 'BBoss_Global_Search_CPT_sql',  
  45. $sql,  
  46. array(  
  47. 'post_type' => $this->cpt_name,  
  48. 'search_term' => $search_term,  
  49. 'only_totalrow_count' => $only_totalrow_count,  
  50. )  
  51. ); 
  52.  
  53. protected function generate_html( $template_type='' ) { 
  54. $post_ids = array(); 
  55. foreach( $this->search_results['items'] as $item_id=>$item_html ) { 
  56. $post_ids[] = $item_id; 
  57.  
  58. //now we have all the posts 
  59. //lets do a wp_query and generate html for all posts 
  60. $qry = new WP_Query( array( 'post_type' => $this->cpt_name, 'post__in'=>$post_ids ) ); 
  61. if( $qry->have_posts() ) { 
  62. while( $qry->have_posts() ) { 
  63. $qry->the_post();  
  64. $result = array( 
  65. 'id' => get_the_ID(),  
  66. 'type' => $this->search_type,  
  67. 'title' => get_the_title(),  
  68. 'html' => buddyboss_global_search_buffer_template_part( 'loop/post', $template_type, false ),  
  69. ); 
  70.  
  71. $this->search_results['items'][get_the_ID()] = $result; 
  72. wp_reset_postdata(); 
  73.