Skip to:
Content
Pages
Categories
Search
Top
Bottom

Forum search inside Buddypress group

  • @wp_maybe

    Participant

    I am trying to implement a search box inside a Buddypress group that searches on the contents of the Group Forum. I found some helpful instructions here

    Basically, I pasted the following code outside the loop in the “content-single-forum.php” file. (Of course this was done to a copy of the bbpress file which I imported into my theme’s bbpress folder):

    <h3>Search Forum</h3>
    <form role="search" method="get" id="bbp-searchform" action="">
    <label for="ts">Search the support forum topics</label>
    <input type="text" name="ts" id="ts" value="<?php echo ( isset( $_REQUEST['ts'] ) ) ? $_REQUEST['ts'] : ''; ?>" />
    <input type="submit" id="searchsubmit" value="Search" />
    </form>

    This code however, performs search only on the topics and not the replies.

    Can someone suggest appropriate modifications to the code so that it searches on both topics and replies rather than just the topics.

    Many thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • @wp_maybe

    Participant

    Does anyone know of any way to perform search on single bbpress forums? Has anyone implemented such a search functionality? Any helpful resources you can suggest?

    Thank you

    @tobyhawkins

    Participant

    I haven’t tried this out, but it looks like it might be what you’re looking for.

    http://sevenspark.com/tutorials/how-to-search-a-single-forum-with-bbpress

    @wp_maybe

    Participant

    Thank you @tobyhawkins! Yes I have gone through that link, and it almost works…

    There is a slight caveat: the searches made on private Buddypress group forums do not return results if the user is below the “bbp_moderator” level. All such users see is the dreaded “Oh bother! No topics were found here” message.

    My guess is that default bbpress search (which sevenspark’s method also employs) behaviour is currently set to not return results from private Buddypress forums for (say) a “bbp_participant” level user.

    The method does however seem to work fine if one is only using sitewide forums and not Buddypress group forums.

    Its a very useful trick, nonetheless!

    @tobyhawkins

    Participant

    Yes. That’s not ideal. I think the problem is the bbp_pre_get_posts_normalize_forum_visibility function in bbpress > includes > forums > functions.php.

    I’m not going to vouch for the security of this (although I think the logic is ok and cursory testing seems to bear this out) but you can get round this by changing your my_bbp_filter_search_results function to the following:

    function my_bbp_filter_search_results( $r ){
    
        //Get the submitted forum ID (added in gethub > bbpress > form-search.php)
        $forum_id = sanitize_title_for_query( $_GET['bbp_search_forum_id'] );
     
        //If the forum ID exits, filter the query
        if( $forum_id && is_numeric( $forum_id ) ){
     
            $r['meta_query'] = array(
                array(
                    'key' => '_bbp_forum_id',
                    'value' => $forum_id,
                    'compare' => '=',
                )
            );
    	    
            $group_id = bbp_get_forum_group_ids( $forum_id );
            if( groups_is_user_member( bp_loggedin_user_id(), $group_id[0] ) )
            {
                function my_allow_all_forums () { return true; }
                add_filter( 'bbp_include_all_forums', 'my_allow_all_forums' );
            }
        }
     
        return $r;
    }
    add_filter( 'bbp_after_has_search_results_parse_args' , 'my_bbp_filter_search_results' );

    To negate the danger of plugins forgetting to manage permissions for posts properly the normalize function visibility automatically steps in on any WP_Query activity and shuts it down, but apparently doesn’t account for the possibility of forums being private, but viewable by low level users within the group context. By adding the filter only when searching on a single forum and then only if the logged-in user is a member of the correct group I think I have accounted for the security issues, but I’m not enough of a php expert to be 100% confident that I haven’t missed something.

    @wp_maybe

    Participant

    Why, this is fabulous! Thank you so much for your help!!

    I tested with your modified code for about an hour, and can’t seem to find any issues. I will post an update in case I do discover any problems…

    But this does appear to be the solution to searching single bbpress forums, including private ones in Buddypress. Thanks to you, @tobyhawkins! 🙂

    @jeffacubed

    Participant

    Folks – I realize this post is a now a few years old, but I’m embarking on doing “exactly” what @wp_maybe outlined in his original post “I am trying to implement a search box inside a Buddypress group that searches on the contents of the Group Forum.”

    I’ve looked through all the code-modifications, suggestions by @tobyhawkins + links (on sevenspark.com above…) – so I’m now just putting things in place to build the search within individual BuddyPress Group Forums (for Public, Private & Hidden BP Groups).

    Q: Was there a final, complete consensus of “exactly” what’s req’d to accomplish this? I’m only asking b/c I don’t what to re-invent the wheel via a slew of testing w/ the various code-bits above.

    Any input or insights would be swell, -Jeff

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar