Group forum search for buddypress

This is Chris Mavricos’s solution extended to enable single forum search inside Buddypress groups.

Step 1. Create a blank file called form-search.php in your theme’s bbpress folder. Put the following code inside this file (your-theme/bbpress/form-search.php):


 * Search
 * @package bbPress
 * @subpackage Theme
$forum_id = bbp_get_forum_id();

<form role="search" method="get" id="bbp-search-form" action="<?php bbp_search_url(); ?>">
        <label class="screen-reader-text hidden" for="bbp_search"><?php _e( 'Search for:', 'bbpress' ); ?></label>
        <input tabindex="<?php bbp_tab_index(); ?>" type="text" value="<?php echo esc_attr( bbp_get_search_terms() ); ?>" name="bbp_search" id="bbp_search" />
        <?php if( $forum_id ): ?>
        <input class="button" type="hidden" name="bbp_search_forum_id" value="<?php echo $forum_id; ?>" />
        <?php endif; ?>
        <input tabindex="<?php bbp_tab_index(); ?>" class="button" type="submit" id="bbp_search_submit" value="<?php esc_attr_e( 'Search', 'bbpress' ); ?>" />

Step 2. Put the following code inside your theme’s functions.php:

 * Search only a specific forum
function my_bbp_filter_search_results( $r ){

    //Get the submitted forum ID (from the hidden field added in step 1)
    $forum_id = sanitize_title_for_query( $_GET['bbp_search_forum_id'] );

    //If the forum ID exits, filter the query
    if( $forum_id && is_numeric( $forum_id ) ){

        $r['meta_query'] = array(
                'key' => '_bbp_forum_id',
                'value' => $forum_id,
                'compare' => '=',

        $group_id = bbp_get_forum_group_ids( $forum_id );
        if( groups_is_user_member( bp_loggedin_user_id(), $group_id[0] ) )
            function my_allow_all_forums () { return true; }
            add_filter( 'bbp_include_all_forums', 'my_allow_all_forums' );

    return $r;
add_filter( 'bbp_after_has_search_results_parse_args' , 'my_bbp_filter_search_results' );

You should find a functional search box inside Buddypress group forums now.

Thanks to tobyhawkins for providing the additional code snippet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.