Skip to:

Opened 3 years ago

Closed 16 months ago

Last modified 16 months ago

#1575 closed enhancement (fixed)

Introduce dedicated forum search page with associated template logic

Reported by: funnycat777 Owned by: jmdodd
Milestone: 2.3 Priority: high
Severity: normal Version:
Component: Search Keywords: has-patch needs-testing
Cc: jared@…, stephen@…, jmdodd@…, vpundir@…, wordpress@…, pippin@…


The search field in the twentyten theme does not search for terms inside the forum, but only inside wordpress. Please let me know how to enable searching within the forum.

Attachments (8)

1575.1.diff (28.4 KB) - added by jmdodd 17 months ago.
Rough first pass.
1575.1.2.diff (27.8 KB) - added by jmdodd 17 months ago.
Rough first pass.
1575.2.diff (34.6 KB) - added by jmdodd 17 months ago.
Code cleanup and commenting, bug fixes, added search form and widget.
1575.patch (33.2 KB) - added by johnjamesjacoby 17 months ago.
1575.4.diff (38.9 KB) - added by jmdodd 17 months ago.
get_the_ID() replaced, search functions broken into separate directory.
1575.5.diff (50.6 KB) - added by jmdodd 16 months ago.
Improved templates and error handling, pagination.
1575.6.diff (48.6 KB) - added by johnjamesjacoby 16 months ago.
Fix search pagination rules
1575.7.diff (52.0 KB) - added by jmdodd 16 months ago.

Download all attachments as: .zip

Change History (36)

comment:1 cnorris233 years ago

Search hasn't been fleshed out at this point. For now, you can enable it, but it will still search regular WP posts/pages. Put something like this in your theme's functions.php

function my_add_bbp_to_search( $post_type ) {
     $post_type['exclude_from_search'] = false;

     return $post_type;
add_filter( 'bbp_register_forum_post_type', 'my_add_search_to_bbp' );
add_filter( 'bbp_register_topic_post_type', 'my_add_search_to_bbp' );
add_filter( 'bbp_register_reply_post_type', 'my_add_search_to_bbp' );

comment:2 johnjamesjacoby3 years ago

There were complaints of forum topics and replies appearing in the blog search results, so I removed them for now. cnorris23's method above is how to get them back together again.

comment:3 johnjamesjacoby3 years ago

  • Milestone changed from Awaiting Review to 2.1
  • Type changed from defect to enhancement

Going to move this to 2.1 incase anyone wants to be creative. :)

comment:4 johnjamesjacoby3 years ago

  • Component changed from Widgets to Search

comment:5 tntc19783 years ago

This is a lifesaver, thanks a bunch cnorris23. There's a little correction to the above code, it should be:

function my_add_bbp_to_search( $post_type ) {
     $post_type['exclude_from_search'] = false;

     return $post_type;
add_filter( 'bbp_register_forum_post_type', 'my_add_bbp_to_search' );
add_filter( 'bbp_register_topic_post_type', 'my_add_bbp_to_search' );
add_filter( 'bbp_register_reply_post_type', 'my_add_bbp_to_search' );

The filters had a wrong reference to the function.

There's still three things "wrong":

  1. It shows the entire topic or reply in the searchresult. If you click the topic nothing is wrong, but if you click the reply, it is shown really weird with a commentform under each reply.
  1. Pagination doesn't work, so you're only shown 1 page of searchresults
  1. No blogposts are shown

But I haven't the skills to fix these issues.

Last edited 3 years ago by tntc1978 (previous) (diff)

comment:6 johnjamesjacoby3 years ago

  • Version 2.0 deleted

comment:7 follow-up: steveorevo3 years ago

  • Keywords has-patch added
  • Priority changed from normal to high

This is because bbPress will override the native WordPress search.php template page in favor of bbPress.php. You'll need to hook the bbp_get_theme_compat_templates as well to force bbPress to return the results to your theme's search.php, which is nicely formatted to display the results in a loop (hence, why you are getting only one search result).

Here is the code (including your fix) to create a really nice unified search result page with bbPress 2.0. Search is essential to any forum and this will do the trick:

function my_add_bbp_to_search( $post_type ) {
     $post_type['exclude_from_search'] = false;
     return $post_type;
add_filter( 'bbp_register_forum_post_type', 'my_add_bbp_to_search' );
add_filter( 'bbp_register_topic_post_type', 'my_add_bbp_to_search' );
add_filter( 'bbp_register_reply_post_type', 'my_add_bbp_to_search' );

function my_bbp_get_theme_compat_templates($templates){
    if ( isset($_GET['s']) ) {
        array_unshift($templates, 'search.php');
    return $templates;
add_filter( 'bbp_get_theme_compat_templates', 'my_bbp_get_theme_compat_templates');

comment:8 in reply to: ↑ 7 steveorevo3 years ago

Created a new plugin to address this. It will also massage the search results with the proper bbPress permalinks. Was hoping for it to work in BP 1.5 too, but just realized that it's not using 2.0 when forums are setup with Groups? Overall, search in BP looks like it needs some work. Will look into it if I can make next months rent (ha ha). Here's the plugin code, may want to include it in core? Will upload it to the plugin repository, time willing...

Plugin Name: Search bbPress 2.0
Plugin URI:
Description: Adds bbPress 2.0 to WordPress search results with links back to the forum, topic, and replies.
Version: 1.0
Author: Stephen Carroll
Author URI:
License: OpenSource under GPL2

// Create our main bbPress Search object
global $bbpsearch;
$bbpsearch = new bbPressSearch();

// Define our main Content Filter class
class bbPressSearch {
    // Hook into the WordPres Shortcode API in our constructor
    function __construct(){

        // Add bbPress to WordPress search results
        add_filter( 'bbp_get_theme_compat_templates', array(&$this, 'bbp_get_theme_compat_templates') );
        add_filter( 'bbp_register_forum_post_type', array(&$this, 'bbp_register_search') );
        add_filter( 'bbp_register_topic_post_type', array(&$this, 'bbp_register_search') );
        add_filter( 'bbp_register_reply_post_type', array(&$this, 'bbp_register_search') );
        add_filter( 'the_permalink', array(&$this, 'the_permalink') );
    function the_permalink($post_link){
        // Remap forum replies' permalink to proper reply url (forum topic#post)
        global $post;
        if ($post->post_type == 'reply') {
            $post_link = bbp_get_reply_url($post->ID);
            // Omit 'reply' link that maps to incorrect post template
            $post->comment_status = "closed";
        return $post_link;
    function bbp_get_theme_compat_templates($templates){
        // Tell bbPress to favor your template's native search.php when looking at results
        if ( isset($_GET['s']) ) {
            array_unshift($templates, 'search.php');
        return $templates;
    function bbp_register_search($post_type){
        // Include bbPress in search results
        $post_type['exclude_from_search'] = false;
        return $post_type;

comment:9 jaredatch3 years ago

  • Cc jared@… added

comment:10 johnjamesjacoby2 years ago

  • Milestone changed from 2.1 to Future Release

Needs to be a dedicated unified search template that overloads the default. Probably needs to be punted to 2.2.

comment:11 netweb2 years ago

  • Cc stephen@… added

comment:12 johnjamesjacoby17 months ago

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from Future Release to 2.3

Quick update on this...

This requires a new template part to load replies, in a loop, that also includes some link to the topic they are in. What this looks like, and how it works, could be anything. We'd likely also want to use this template for the User > Replies Created page, since right now there's no context to what those replies are actually replying to.

comment:13 jmdodd17 months ago

  • Cc jmdodd@… added

comment:14 sooskriszta17 months ago

  • Cc vpundir@… added

See also #1956

comment:15 MZAWeb17 months ago

  • Cc wordpress@… added

comment:16 johnjamesjacoby17 months ago

  • Owner set to jmdodd

jmdodd17 months ago

Rough first pass.

comment:17 jmdodd17 months ago

Todo on second pass:

  • Search form
  • Search form widget
  • Double-check pagination
  • Start looking at BuddyPress integration
  • Better templates/template file structure
  • Default search page if no results/no search

jmdodd17 months ago

Rough first pass.

jmdodd17 months ago

Code cleanup and commenting, bug fixes, added search form and widget.

comment:18 jmdodd17 months ago

  • Keywords has-patch added; needs-patch removed

In 1575.2.diff, I think I'm on the right track with the permastruct rewrites. They are based on the WordPress search. I've also added a search form widget and rudimentary search result templates to facilitate testing.

comment:19 johnjamesjacoby17 months ago

Patch is looking like a great first pass.

  • Uses get_the_ID() in the templates so they put out correct results. Is it worth modifying bbp_get_reply_id(), bbp_get_topic_id(), and bbp_get_forum_id() to use get_the_ID() correctly if in a search query?
  • Will need pagination, which is moderately annoying because the rules are more complex. (Use the topic views as an example if you need to.)
  • The templates need iterating to look more like forum post results. Thinking something similar to loop-single-reply.php, but separated out.
  • /forums/search/ should resolve correctly, even if there's no query string. That way there's a page with a form all the time, even if the widget is not used.
  • I think the loop stuff should be extracted into its own file. Do you think we'll need several files similar to the other components? Do you see search needing capabilities, functions, template tags, etc? (/includes/search/*.*)
  • It runs quite a few queries for each result; wonder what we'll be able to do to optimize it later.

New patch uses get_the_ID() to make the testing templates work as intended.

Last edited 17 months ago by johnjamesjacoby (previous) (diff)

johnjamesjacoby17 months ago

comment:20 johnjamesjacoby17 months ago

Shoot. Didn't notice the .diff extention. Above patch is a 3rd iteration, based on the 2nd.

comment:21 jmdodd17 months ago

I think on the next pass I'm going to break search into a separate directory in /includes. It has ceased being common, and is now more similar in scope to replies, topics, users, etc.

comment:22 mordauk17 months ago

  • Cc pippin@… added

jmdodd17 months ago

get_the_ID() replaced, search functions broken into separate directory.

comment:23 jmdodd17 months ago

Working through templates now, should have more complete versions up this afternoon.

/topics/view does not resolve, so I'm going to have to figure out how to get a default empty search template for forums/search working properly. Right now it gives me the main blog, so I'm going to have to dig into what it thinks it is, versus what I want it to be.

comment:24 johnjamesjacoby16 months ago

  • Keywords needs-testing added
  • Summary changed from About Search Field in BBPress 2.0 Beta 3 to Introduce dedicated forum search page with associated template logic

jmdodd16 months ago

Improved templates and error handling, pagination.

comment:25 jmdodd16 months ago

Templates are more in line with bbPress standards, although they're going to need a second pass just to double-check style names and CSS sanity. Search has pagination and notification of no results.

To do:

  • I think that for results from private forums, search results should substitute a privacy notice for the content, but leave the title intact. Thoughts?
  • Still wrestling with the 404 from /forums/search.

johnjamesjacoby16 months ago

Fix search pagination rules

jmdodd16 months ago

comment:26 jmdodd16 months ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [4579]) Introduce dedicated forum search.

  • Search forums, topics, and replies.
  • Add new search functions, including bbp_has_search_results().
  • Provide templates for search results.
  • Fixes #1575.

comment:27 johnjamesjacoby16 months ago

(In [4618]) Search Tweaks:

  • Add support for search root page.
  • Add search root page to breadcrumb.
  • Tweak search form CSS.
  • See #1575.

comment:28 johnjamesjacoby16 months ago

(In [4619]) RTL CSS tweaks for search. See #1575.

Note: See TracTickets for help on using tickets.