Adding Custom Fields to bbPress Topics, then Filtering Topics by Custom Fields using Views

Posted in bbPress, Website Tips, Wordpress

Adding Custom Fields to bbPress Topics, then Filtering Topics by Custom Fields using Views

I am creating a rideshare site. My goal was to add custom fields to bbPress topics, and then filter those topics in order to display relevant information to the user.

For example, I want people offering rides to be able to select their “Destination” from a dropdown list. Then, I want users to be able to filter the offered rides by destination.

1. Create the custom bbPress topic fields

To create the custom bbPress fields, I used a premium plugin called “GD Custom Posts and Taxonomy Tools“, which has special bbPress functions. See the plugin author’s bbPress custom metabox tutorial here. I’m sure you can custom code this, too. You’ll have to configure this plugin, add your custom fields, and add your metaboxes before continuing.

In the screenshot below, you can see the droplist displaying 3 destination options. Users offering a ride must choose one. Later, we will sort by this destination value.

Custom field in a bbPress topic form. Created with GD Custom Tools and Taxonomies Premium Plugin.

2. Filter topics by custom meta value using the bbPviews Plugin (bbPviews tutorial)

Download the plugin and install it. By default, the plugin gives you 2 additional “views”, Most Popular topics and Least Popular topics. But we’re going to add our own “View” to the plugin.

3. Add “Views” Widget to your Sidebar

Go to Appearance > Widgets and drag the “bbPress Views List” widget to your sidebar. Refresh a page, and you should see 3 views listed: Topics with no replies, Popular Topics, and Unpopular Topics.

4. Add Our Custom “View”, which will filter bbPress topics by custom meta_key value

Go to the bbPviews plugin file in plugins > bbpviews > bbpviews.php. Here is what you’ll see by default:

function wphc_register_custom_views() {
bbp_register_view( 'popular-topics', __( 'Popular Topics' ), array( 'meta_key' => '_bbp_reply_count', 'orderby' => 'meta_value_num' ), false );
bbp_register_view( 'unpopular-topics', __( 'Unpopular Topics' ), array( 'meta_key' => '_bbp_reply_count', 'orderby' => 'meta_value_num', 'order' => 'asc' ), false );
}
add_action( 'bbp_register_views', 'wphc_register_custom_views' );

Now, add your custom view, like this:

function wphc_register_custom_views() {
bbp_register_view( 'popular-topics', __( 'Popular Topics' ), array( 'meta_key' => '_bbp_reply_count', 'orderby' => 'meta_value_num' ), false );
bbp_register_view( 'unpopular-topics', __( 'Unpopular Topics' ), array( 'meta_key' => '_bbp_reply_count', 'orderby' => 'meta_value_num', 'order' => 'asc' ), false );
bbp_register_view( 'custom-view', __( 'Custom View' ), array( 'meta_key' => 'your_custom_metakey_name', 'meta_value' => 'your_custom_metakey_value' ), false );
}
add_action( 'bbp_register_views', 'wphc_register_custom_views' );

Replace:
custom-view –> your custom slug

Custom View –> your custom name for the view (will be displayed to the public)

your_custom_metakey_name –> the custom metakey you created. If you’re using the GD Custom Posts and Taxonomies Plugin like me, this metakey name can be found by going to the GD Plugin Settings > metaboxes. The values in the column labeled “code” are your metakey values.

your_custom_metakey_value –> the value you want to filter by. So for me, if I wanted to filter by “ride_destination”, which is custom metakey used by the droplist above. And I wanted to display all topics where ride_destination = “Mt. Hood Meadows”. So this is what my bbPviews.php file looks like:


function wphc_register_custom_views() {
bbp_register_view( 'popular-topics', __( 'Popular Topics' ), array( 'meta_key' => '_bbp_reply_count', 'orderby' => 'meta_value_num' ), false );
bbp_register_view( 'unpopular-topics', __( 'Unpopular Topics' ), array( 'meta_key' => '_bbp_reply_count', 'orderby' => 'meta_value_num', 'order' => 'asc' ), false );
bbp_register_view( 'destination-meadows', __( 'Mt. Hood Meadows' ), array( 'meta_key' => 'ride_destination', 'meta_value' => 'Mt. Hood Meadows' ), false );
}
add_action( 'bbp_register_views', 'wphc_register_custom_views' );

5. Done!

Here’s the final product. Notice the “Mt. Hood Meadows” view in the sidebar. The topics shown are the results of clicking that link. The URL of this page is: http://[your-domain-name]/forums/view/destination-meadows/

That took me a long time, so I hope this will help someone!
- Alan