bbPress: Keep Forum Posts Out of Site Search
Removes forums, topics and replies from your main WordPress search results so blog searches stay focused; bbPress's own forum search still works.
Opens WPIDE on
Code
if ( ! function_exists( 'bbp_get_topic_post_type' ) ) {
return;
}
add_action( 'pre_get_posts', function ( $query ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() || bbp_is_search() ) {
return;
}
$types = $query->get( 'post_type' );
if ( ! $types || 'any' === $types ) {
$types = get_post_types( array( 'exclude_from_search' => false ) );
}
$query->set( 'post_type', array_values( array_diff( (array) $types, array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() ) ) ) );
} );
Notes
The forum search box (bbPress search) is not affected.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.