Blog: WordPress
Young woman working at laptop

bbPress: Add Forums, Topics & Replies to Search Results

Avatar for John Locke

John Locke is a SEO consultant from Sacramento, CA. He helps manufacturing businesses rank higher through his web agency, Lockedown Design & SEO.

Many people who use bbPress may notice that WordPress by default doesn’t return topics, replies, or forums in search results.

This can defeat the purpose of having forums on your site, if they aren’t easily searchable.

This is a code snippet that will add forums, topics, and replies from bbPress to your WordPress search results.

Bear in mind, some premium themes have bbPress bundled with the theme, and may have their own custom templates for displaying search results. We’ll cover that in a second.

For now, here’t the code block you’ll want to add to your functions.php file.



/**
 * Include bbPress 'topic' custom post type in WordPress' search results 
 */
 
function ntwb_bbp_topic_cpt_search( $topic_search ) {
	$topic_search['exclude_from_search'] = false;
	return $topic_search;
}
add_filter( 'bbp_register_topic_post_type', 'ntwb_bbp_topic_cpt_search' );

/**
 * Include bbPress 'forum' custom post type in WordPress' search results 
 */

function ntwb_bbp_forum_cpt_search( $forum_search ) {
	$forum_search['exclude_from_search'] = false;
	return $forum_search;
}
add_filter( 'bbp_register_forum_post_type', 'ntwb_bbp_forum_cpt_search' );

/**
 * Include bbPress 'reply' custom post type in WordPress' search results 
 */

function ntwb_bbp_reply_cpt_search( $reply_search ) {
	$reply_search['exclude_from_search'] = false;
	return $reply_search;
}
add_filter( 'bbp_register_reply_post_type', 'ntwb_bbp_reply_cpt_search' );



Dealing With Search in Premium Themes

Like I mentioned earlier, some themes — like the kind you would pick up at ThemeForest — often have bbPress included in the theme, and they may output search results according to their own internal functions.

One theme I encountered recently outputs search results via a shortcode buried in their internal theme folders. The specific shortcode started like this:


$query_options = array(
    'post_type' => array('post', 'page'),
    /* More stuff here*/

);

As a result, the search count result was thrown off, because even though the forums and topics were being returned in search results, the results count wasn’t reflecting those results accurately.

Now your theme might vary, but what I did to fix the results count was override the shortcode in a child theme, like this:


$query_options = array(
    'post_type' => array('post', 'forum', 'topic', 'reply', 'page'),
    /* More stuff here*/

);

After that, the search results count was accurate in this particular premium theme. If you’re encountering this problem, you may have to find the file in the theme that controls this value.

Avatar for John Locke

John Locke is a SEO consultant from Sacramento, CA. He helps manufacturing businesses rank higher through his web agency, Lockedown Design & SEO.

Join the Conversation

Your email address will be kept private. Required fields marked *.