Note: You are viewing the documentation for Version 4View Version 3 Docs

Limiting Search Results to the Active bbPress Forum

Some bbPress forum installs are huge. Perhaps you’d like to help out your visitors by limiting the on-page search to the Forum currently being viewed. We can customize the output of get_search_form() (which includes the WordPress Search (not bbPress) Widget) to only consider the bbPress Forum currently on display when searching:

All hooks should be added to your custom SearchWP Customizations Plugin.

<?php
// Customize native WordPress search form to include current bbPress Forum ID if applicable.
add_filter( 'get_search_form', function( $markup ) {
if ( ! function_exists( 'bbp_get_forum_id' ) ) {
return $markup;
}
$forum_id = isset( $_REQUEST['swpforumid'] ) ? absint( $_REQUEST['swpforumid'] ) : bbp_get_forum_id();
if ( empty( $forum_id ) ) {
return $markup;
}
return str_replace( '</form>',
'<input type="hidden" name="swpforumid" value="' . esc_attr( $forum_id ) . '" /></form>',
$markup
);
} );
// Limit SearchWP results to the current bbPress Forum when applicable.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
if ( ! isset( $_REQUEST['swpforumid'] ) ) {
return $mods;
}
$mod = new \SearchWP\Mod(
\SearchWP\Utils::get_post_type_source_name( 'post' )
);
$mod->set_where( [ [
'column' => 'id',
'value' => absint( $_REQUEST['swpforumid'] ),
'compare' => '=',
'type' => 'NUMERIC',
] ] );
$mods[] = $mod;
return $mods;
}, 30, 2 );
Fix keyword search on your site. No coding required!

Now you can utilize all of the content that's gone unrecognized by native WordPress keyword search instantly with SearchWP

Get SearchWP