How to search a single forum with bbPress

Out of the box, bbPress search will search across all forums. However, there are use cases where this doesn’t make sense, one being running a support forum. For example, if you’re having an issue with Product A and are searching for a solution, you only want to see results from Product A’s forum, not results from Product B’s forum.

A specific case from my support forums: if an UberMenu customer searches the forum for “responsive”, they should get results from the UberMenu – WordPress Mega Menu Plugin forum but not from the Agility Responsive WordPress Theme forum.

Since I couldn’t find a setting or plugin to accomplish this, I made a few customizations to accomplish the following:

  1. Display a search bar on the individual product forums (/forum/ubermenu).
  2. Automatically restrict the search form on each forum to return results only from that forum’s topics.

To implement this we need to make three customizations:

  1. Move the search form to the top of the single forum template
  2. Add an extra field to the search form to identify the current forum
  3. Filter the search query to only search a specific forum

Before proceeding, we’ll be overriding bbPress templates in our theme, so you’ll want to set up a child theme with a /bbpress subfolder to house these templates. bbPress’ default templates are located in the wp-content/plugins/bbpress/templates/defaults/bbpress directory, and we’ll copy the templates we need from here to our wp-content/themes/{mychildtheme}/bbpress directory in order to override them.

Step 1: Move the search form to the individual forums

By default, the bbPress search form will appear at the top of the main /forums page, produced by the content-archive-forum.php template. There is no hook to remove the search form, so we’ll do this by overriding the template. Copy bbPress’ content-archive-forum.php template into your child theme’s /bbpress directory, then remove the search box from the top of the file. Here’s the code you’ll remove:

<div class="bbp-search-form">

	<?php bbp_get_template_part( 'form', 'search' ); ?>

</div>

To move the search form into the content-single-forum.php template, however, we can just use an action hook rather than needing to override the template (which could lead to extra maintenance down the road). Just add this action to your child theme’s functions.php

function my_bbp_search_form(){
	?>
	<div class="bbp-search-form">

		<?php bbp_get_template_part( 'form', 'search' ); ?>

	</div>
	<?php
}
add_action( 'bbp_template_before_single_forum', 'my_bbp_search_form' );

The search form should now appear on your individual forums rather than on the main forums page.

Step 2: Add a field to the search form to identify the current forum

In order to restrict search results to a particular forum, we’ll need that forum’s ID when the form is submitted to alter the query. In this case we need to override form-search.php, so copy bbPress’s template into the child theme’s /bbpress directory, then make the following alterations:

1. Get the forum ID

$forum_id = bbp_get_forum_id();

2. After the search field, add a hidden field to store the forum ID

<?php if( $forum_id ): ?>
	<input class="button" type="hidden" name="bbp_search_forum_id" value="<?php echo $forum_id; ?>" />
<?php endif; ?>

3. (Optional) Edit the placeholder text on the search field to display “Search {Forum Title} Topics”

<input placeholder="Search <?php the_title(); ?> Topics" tabindex="<?php bbp_tab_index(); ?>" type="text" value="<?php echo esc_attr( bbp_get_search_terms() ); ?>" name="bbp_search" id="bbp_search" />

The final file looks like this:

/**
 * 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(); ?>">
	<div>
		<label class="screen-reader-text hidden" for="bbp_search"><?php _e( 'Search for:', 'bbpress' ); ?></label>
		<input placeholder="Search <?php the_title(); ?> Topics" 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' ); ?>" />
	</div>
</form>

Nothing will change visibly on your site after making these changes, but now the forum ID value will be submitted when the search button is clicked.

Step 3: Filter the search query to only search a specific forum

Here’s where the real meat of the customization occurs. Using the bbp_after_has_search_results_parse_args filter we’ll alter the search query arguments and add a meta query to restrict the results to topics/replies in a specific forum. Just add this code to your 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 2)
	$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(
			array(
				'key' => '_bbp_forum_id',
				'value' => $forum_id,
				'compare' => '=',
			)
		);
		
	}

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

That’s it! Now when you search from a search form within a specific forum, you’ll only get results back from that forum.

8 thoughts on “How to search a single forum with bbPress

  1. The hidden input action “bbp-search-request” also needs to be commented out, or removed from “form-search.php”. You removed it from your “final code”, but didn’t make note of it in your steps. Great, simple solution for exactly what I was looking for. Thanks!

    • Thanks for the heads-up. I’m thinking maybe that field didn’t exist when the article was originally written. Glad it helped! 🙂

  2. Thanks for the above – it works a treat but is a little restrictive – is there any way to get it to search sub-forums as well (I’m very new to all this and wouldn’t know where to start looking).

  3. That’s superb! Thanks Chris

    Although like Tim, I’d like it to search sub/child forums, is this possible? That was the main reason for me finding your fix if I’m honest! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *