/includes/plugins/search-cpt/class.BBoss_Global_Search_CPT.php

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