Skip to:
Content

Opened 6 years ago

Closed 4 years ago

Last modified 4 years 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: Component - Search Keywords: has-patch needs-testing
Cc: jared@…, stephen@…, jmdodd@…, vpundir@…, wordpress@…, pippin@…

Description

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 4 years ago.
Rough first pass.
1575.1.2.diff (27.8 KB) - added by jmdodd 4 years ago.
Rough first pass.
1575.2.diff (34.6 KB) - added by jmdodd 4 years ago.
Code cleanup and commenting, bug fixes, added search form and widget.
1575.patch (33.2 KB) - added by johnjamesjacoby 4 years ago.
1575.4.diff (38.9 KB) - added by jmdodd 4 years ago.
get_the_ID() replaced, search functions broken into separate directory.
1575.5.diff (50.6 KB) - added by jmdodd 4 years ago.
Improved templates and error handling, pagination.
1575.6.diff (48.6 KB) - added by johnjamesjacoby 4 years ago.
Fix search pagination rules
1575.7.diff (52.0 KB) - added by jmdodd 4 years ago.

Download all attachments as: .zip

Change History (36)

#1 @cnorris23
6 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' );

#2 @johnjamesjacoby
6 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.

#3 @johnjamesjacoby
6 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. :)

#4 @johnjamesjacoby
6 years ago

  • Component changed from Widgets to Search

#5 @tntc1978
6 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 6 years ago by tntc1978 (previous) (diff)

#6 @johnjamesjacoby
6 years ago

  • Version 2.0 deleted

#7 follow-up: @steveorevo
6 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');

#8 in reply to: ↑ 7 @steveorevo
6 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: http://www.serverpress.com/products/search-bbpress
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: http://www.steveorevo.com
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;
    }
}

#9 @jaredatch
6 years ago

  • Cc jared@… added

#10 @johnjamesjacoby
6 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.

#11 @netweb
5 years ago

  • Cc stephen@… added

#12 @johnjamesjacoby
5 years 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.

#13 @jmdodd
5 years ago

  • Cc jmdodd@… added

#14 @sooskriszta
5 years ago

  • Cc vpundir@… added

See also #1956

#15 @MZAWeb
5 years ago

  • Cc wordpress@… added

#16 @johnjamesjacoby
4 years ago

  • Owner set to jmdodd

@jmdodd
4 years ago

Rough first pass.

#17 @jmdodd
4 years 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

@jmdodd
4 years ago

Rough first pass.

@jmdodd
4 years ago

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

#18 @jmdodd
4 years 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.

#19 @johnjamesjacoby
4 years 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 4 years ago by johnjamesjacoby (previous) (diff)

#20 @johnjamesjacoby
4 years ago

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

#21 @jmdodd
4 years 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.

#22 @mordauk
4 years ago

  • Cc pippin@… added

@jmdodd
4 years ago

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

#23 @jmdodd
4 years 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.

#24 @johnjamesjacoby
4 years 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

@jmdodd
4 years ago

Improved templates and error handling, pagination.

#25 @jmdodd
4 years 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.

@johnjamesjacoby
4 years ago

Fix search pagination rules

@jmdodd
4 years ago

#26 @jmdodd
4 years 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.

#27 @johnjamesjacoby
4 years ago

(In [4618]) Search Tweaks:

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

#28 @johnjamesjacoby
4 years ago

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

Note: See TracTickets for help on using tickets.