Add Search to bbPress WordPress Plugin

Posted by & filed under bbPress Tutorials, Developing for WordPress, How To's, Inter, Plugins.

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:


<!--?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?-->

// Content inside the loop

<!--?php endwhile; else: ?-->

<!--?php _e('Sorry, no posts matched your criteria.'); ?-->

<!--?php endif; ?-->

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

<tr>
	<td><a href="<?php the_permalink() ?>><?php the_title(); ?></a></td>

	<td><?php the_time('l jS F, Y - g:ia') ?></td>
</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.

<?php if (have_posts()) : ?>

<h1><?php printf( __( 'Search Results for: %s' ), '<span>' . get_search_query() . '</span>' ); ?></h1>

<table>
	<thead>
		<tr>
			<th>Title</th>
			<th>Date</th>
		</tr>
	</thead>

	<tbody>

<?php while (have_posts()) : the_post(); ?>

<tr>
	<td><a href="<?php the_permalink() ?>><?php the_title(); ?></a></td>

	<td><?php the_time('l jS F, Y - g:ia') ?></td>
</tr>

<?php endwhile; ?>

<?php endif; ?>

	</tbody>

</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.

<?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.

<div class="search-results-wrap">

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

<?php if (have_posts()) : ?>

<h1><?php printf( __( 'Search Results for: %s' ), '<span>' . get_search_query() . '</span>' ); ?></h1>

<table>
	<thead>
		<tr>
			<th>Title</th>
			<th>Date</th>
		</tr>
	</thead>

	<tbody>

<?php while (have_posts()) : the_post(); ?>

<tr>
	<td><a href="<?php the_permalink() ?>><?php the_title(); ?></a></td>

	<td><?php the_time('l jS F, Y - g:ia') ?></td>
</tr>

<?php endwhile; ?>

<?php endif; ?>

	</tbody>

</table>

</div>

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

Tags: , ,

Brady Nord

I am the co-founder of Mojoness Inc. and a handful of other good times. I thoroughly enjoy most things with my wife, friends and WordPress.

6 Responses

    • Brady Nord July 26, 2012 at 6:05 pm

      David, I agree with you that the plugin does a great job and is nice to have the forum search separately although in some instances the results were often difficult to render properly and I found it easier to not use the plugin. The point I wanted to make with the tut is to explain a second option if you cannot get the plugin to display results as you would like.

      Thoughts?

  1. web page January 24, 2014 at 10:37 am

    s teeth, hair, and skin, and no tools are required for
    assembly. They mmay also thump too get attention, which might mean they
    want outt of thee cage to plzy and be with thhe family.
    Somehow having their own spac eases the separation
    anxiety that mosdt dogs experience.

    Reply
  2. Younique February 25, 2014 at 10:17 pm

    Basically you will have the right to sell the Younique educational products as
    we as retail. Now you know why you shouldn’t be a tumbleweed in the
    desert. It’s the first thing people see and it’s what
    draws attention to any person.

    Reply
  3. or April 19, 2014 at 10:08 pm

    Hi everyone, it’s my first visit at this web site, and article is truly fruitful in favor of me, keep up posting these types of posts.

    Reply

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>