When working with bbPress I found struggles to get search to work properly, and hopefully after this article you wont have the same struggles I had. I didn’t see any point in building what is already built so I decided to first search the WordPress plugin directory to see what was currently available. After a few different plugins I settled on using Search bbPress from Stephen Carroll. Search bbPress works by extending the default WordPress search to include the bbPress custom post types. Now you know the basics, lets get started below.

This tutorial assumes you want all your theme search results being displayed in a table like your forums are structured.

Step 1:

Download and install the Search bbPress plugin. You can either search for the plugin from your WordPress dashboard or simply go here and download the plugin. Once you have installed the plugin go ahead and activate it. Nothing fancy or exciting will happen (that you can see ;) ) but it’s working.

Step 2:

Go to your widget section under Appearance -> Widgets. Drag the default Search widget to the sidebar that you would like to display it in give it a title and that is it for step 2.

Step 3:

This step is where things could get a little tricky. Since bbPress forums are displayed using tables most likely the results look a little weird since your theme’s search.php doesn’t have the results being displayed in tables. If you think the results are just fine then you are good to go from here.

In my case, I wanted to have my results displayed in tables so I had to open the search.php and style.css files in my editor and make some changes.

Results in a table

First you need to find the loop in the page. The loop should look something like this:

001 <!--?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?-->
003 // Content inside the loop
005 <!--?php endwhile; else: ?-->
007 <!--?php _e('Sorry, no posts matched your criteria.'); ?-->
009 <!--?php endif; ?-->

Since we want each result to be inside a table row, we need to add the following inside our loop.

001 <tr>
002     <td><a href="<?php the_permalink() ?>><?php the_title(); ?></a></td>
004     <td><?php the_time('l jS F, Y - g:ia') ?></td>
005 </tr>

If you are not familiar with tables the tr stands for table row and the td stands for table data. So what this is doing is adding a row with the title and the time inside of the row. Now, we need to finish up the table information that will go outside the loop.

If there are results that are found, we want to display the table and give the table columns a headline. I am going to break up the loop and add a title to the results stating the results for what I searched for.

001 <?php if (have_posts()) : ?>
003 <h1><?php printf( __( 'Search Results for: %s' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
005 <table>
006     <thead>
007         <tr>
008             <th>Title</th>
009             <th>Date</th>
010         </tr>
011     </thead>
013     <tbody>
015 <?php while (have_posts()) : the_post(); ?>
017 <tr>
018     <td><a href="<?php the_permalink() ?>><?php the_title(); ?></a></td>
020     <td><?php the_time('l jS F, Y - g:ia') ?></td>
021 </tr>
023 <?php endwhile; ?>
025 <?php endif; ?>
027     </tbody>
029 </table>

Now you should see your results being displayed just like your forum tables are displayed. Something I would recommend adding to your pages is a simple line of code to limit the number of results on your page.

001 <?php $posts=query_posts($query_string . '&posts_per_page=20'); ?>

You can change the 20 to the number of results you would like to display.

Here is the final code to display your search results.

001 <div class="search-results-wrap">
003 <?php $posts=query_posts($query_string . '&posts_per_page=20'); ?>
005 <?php if (have_posts()) : ?>
007 <h1><?php printf( __( 'Search Results for: %s' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
009 <table>
010     <thead>
011         <tr>
012             <th>Title</th>
013             <th>Date</th>
014         </tr>
015     </thead>
017     <tbody>
019 <?php while (have_posts()) : the_post(); ?>
021 <tr>
022     <td><a href="<?php the_permalink() ?>><?php the_title(); ?></a></td>
024     <td><?php the_time('l jS F, Y - g:ia') ?></td>
025 </tr>
027 <?php endwhile; ?>
029 <?php endif; ?>
031     </tbody>
033 </table>
035 </div>

If you have other questions regarding how to display your forums or how to use bbPress shortcodes, see the following posts: