bbp_has_search_results( mixed $args = '' )

The main search loop. WordPress does the heavy lifting.

Description

Parameters

$args

(Optional) All the arguments supported by WP_Query

Default value: ''

Return

(object) Multidimensional array of search information

Source

File: bp-forums/search/template.php

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
function bbp_has_search_results( $args = '' ) {
    global $wp_rewrite;
 
    /** Defaults **************************************************************/
 
    $default_post_type = array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() );
 
    // Default query args
    $default = array(
        'post_type'           => $default_post_type,         // Forums, topics, and replies
        'posts_per_page'      => bbp_get_replies_per_page(), // This many
        'paged'               => bbp_get_paged(),            // On this page
        'orderby'             => 'date',                     // Sorted by date
        'order'               => 'DESC',                     // Most recent first
        'ignore_sticky_posts' => true,                       // Stickies not supported
        's'                   => bbp_get_search_terms(),     // This is a search
    );
 
    // What are the default allowed statuses (based on user caps)
    if ( bbp_get_view_all() ) {
 
        // Default view=all statuses
        $post_statuses = array(
            bbp_get_public_status_id(),
            bbp_get_closed_status_id(),
            bbp_get_spam_status_id(),
            bbp_get_trash_status_id()
        );
 
        // Add support for private status
        if ( current_user_can( 'read_private_topics' ) ) {
            $post_statuses[] = bbp_get_private_status_id();
        }
 
        // Join post statuses together
        $default['post_status'] = implode( ',', $post_statuses );
 
    // Lean on the 'perm' query var value of 'readable' to provide statuses
    } else {
        $default['perm'] = 'readable';
    }
 
    /** Setup *****************************************************************/
 
    // Parse arguments against default values
    $r = bbp_parse_args( $args, $default, 'has_search_results' );
 
    // Get Forums
    $bbp = bbpress();
 
    // Call the query
    if ( ! empty( $r['s'] ) ) {
        $bbp->search_query = new WP_Query( $r );
    }
 
    // Add pagination values to query object
    $bbp->search_query->posts_per_page = $r['posts_per_page'];
    $bbp->search_query->paged          = $r['paged'];
 
    // Never home, regardless of what parse_query says
    $bbp->search_query->is_home        = false;
 
    // Only add pagination is query returned results
    if ( ! empty( $bbp->search_query->found_posts ) && ! empty( $bbp->search_query->posts_per_page ) ) {
 
        // Array of arguments to add after pagination links
        $add_args = array();
 
        // If pretty permalinks are enabled, make our pagination pretty
        if ( $wp_rewrite->using_permalinks() ) {
 
            // Shortcode territory
            if ( is_page() || is_single() ) {
                $base = trailingslashit( get_permalink() );
 
            // Default search location
            } else {
                $base = trailingslashit( bbp_get_search_results_url() );
            }
 
            // Add pagination base
            $base = $base . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
 
        // Unpretty permalinks
        } else {
            $base = add_query_arg( 'paged', '%#%' );
        }
 
        // Add args
        if ( bbp_get_view_all() ) {
            $add_args['view'] = 'all';
        }
 
        // Add pagination to query object
        $bbp->search_query->pagination_links = paginate_links(
            apply_filters( 'bbp_search_results_pagination', array(
                'base'      => $base,
                'format'    => '',
                'total'     => ceil( (int) $bbp->search_query->found_posts / (int) $r['posts_per_page'] ),
                'current'   => (int) $bbp->search_query->paged,
                'prev_text' => is_rtl() ? '→' : '←',
                'next_text' => is_rtl() ? '←' : '→',
                'mid_size'  => 1,
                'add_args'  => $add_args,
            ) )
        );
 
        // Remove first page from pagination
        if ( $wp_rewrite->using_permalinks() ) {
            $bbp->search_query->pagination_links = str_replace( $wp_rewrite->pagination_base . '/1/', '', $bbp->search_query->pagination_links );
        } else {
            $bbp->search_query->pagination_links = str_replace( '&paged=1', '', $bbp->search_query->pagination_links );
        }
    }
 
    // Return object
    return apply_filters( 'bbp_has_search_results', $bbp->search_query->have_posts(), $bbp->search_query );
}

Changelog

Changelog
Version Description
bbPress (r4579) Introduced.

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.