Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@searchwpgists
Created March 9, 2022 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save searchwpgists/90d3ffabb596cb434c807c9bbf489f2a to your computer and use it in GitHub Desktop.
Save searchwpgists/90d3ffabb596cb434c807c9bbf489f2a to your computer and use it in GitHub Desktop.
Limit SearchWP results to the current bbPress Forum when applicable
<?php
// Customize native WordPress search form to include current bbPress Forum ID if applicable.
// @link https://searchwp.com/documentation/knowledge-base/limiting-search-results-bbpress/
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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment