Bp_Search_bbPress

BuddyPress Global Search – search bbPress class

Description

Source

File: bp-search/classes/class-bp-search-bbpress.php

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
abstract class Bp_Search_bbPress extends Bp_Search_Type {
    public $type;
 
    function sql( $search_term, $only_totalrow_count=false ){
        global $wpdb;
        $query_placeholder = array();
 
        $sql = " SELECT ";
 
        if( $only_totalrow_count ){
            $sql .= " COUNT( DISTINCT id ) ";
        } else {
            $sql .= " DISTINCT id , '{$this->type}' as type, post_title LIKE %s AS relevance, post_date as entry_date  ";
            $query_placeholder[] = '%'. $search_term .'%';
        }
 
        $sql .= " FROM
                    {$wpdb->prefix}posts
                WHERE
                    1=1
                    AND (
                            (
                                    (post_title LIKE %s)
                                OR  (post_content LIKE %s)
                            )
                        )
                    AND post_type = '{$this->type}'
                    AND post_status = 'publish'
            ";
        $query_placeholder[] = '%'. $search_term .'%';
        $query_placeholder[] = '%'. $search_term .'%';
        $sql = $wpdb->prepare( $sql, $query_placeholder );
 
        return apply_filters(
            'Bp_Search_Forums_sql',
            $sql,
            array(
                'search_term'           => $search_term,
                'only_totalrow_count'   => $only_totalrow_count,
            )
        );
    }
 
    protected function generate_html( $template_type='' ){
        $post_ids = array();
        foreach( $this->search_results['items'] as $item_id=>$item_html ){
            $post_ids[] = $item_id;
        }
 
        remove_action( 'pre_get_posts', 'bbp_pre_get_posts_normalize_forum_visibility', 4 );
 
        //now we have all the posts
        //lets do a wp_query and generate html for all posts
        $qry = new WP_Query( [
            'post_type'     => [ 'forum', 'topic', 'reply' ],
            'post__in'      => $post_ids,
            'post_status'   => [ 'publish', 'private', 'hidden' ],
            'no_found_rows' => true,
            'nopaging'      => true
        ] );
 
 
        add_action( 'pre_get_posts', 'bbp_pre_get_posts_normalize_forum_visibility', 4 );
 
        if( $qry->have_posts() ){
            while( $qry->have_posts() ){
                $qry->the_post();
 
                /**
                 * The following will try to load loop/forum.php, loop/topic.php loop/reply.php(if reply is included).
                 *
                 */
                $result_item = array(
                    'id'    => get_the_ID(),
                    'type'  => $this->type,
                    'title' => get_the_title(),
                    'html'  => bp_search_buffer_template_part( 'loop/' . $this->type, $template_type, false ),
                );
 
                $this->search_results['items'][get_the_ID()] = $result_item;
            }
        }
        wp_reset_postdata();
    }
 
}

Methods

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.