Posted on

bbP Private Groups support

Private Groups is a plugin that makes bbPress forum groups private. Relevanssi, however, doesn’t understand that privacy and will show those groups in the search results.

Relevanssi has means to support this, however, with the help of the relevanssi_post_ok filter. Here’s some code from Mark Wass that makes the private forum posts private in search. Add this code to the theme functions.php file.

/*-----------------------------------------------------------------------------------*/ 
/* 
/* Filter search results so that private forum posts are not displayed 
/* to members who do not belong to the particular private group. 
/* 
/*-----------------------------------------------------------------------------------*/
add_filter('relevanssi_post_ok', 'rlv_filter_private_forum_posts', 11, 2);
 
function rlv_filter_private_forum_posts($post_ok, $post_id) {
    //Default user can view results 
    $user_can_view = 1;
 
    $post = get_post($post_id);
 
    //Get the post type 
    $post_type = $post->post_type;
 
    //Get the forum id the post belongs to (Only if it's a topic or reply) 
    if ( $post_type == 'reply' || $post_type == 'topic' ) {
        $forum_id = private_groups_get_forum_id_from_post_id($post_id, $post_type);
    }
 
    //Check if current user can view the forum that the post belongs to. 
    if ( private_groups_can_user_view_post_id($forum_id) === false ) {
        $user_can_view = 0;
    }
 
    //Filter the search results for forum topics or replies based on the users forum permission. 
    if ( $post_type == 'reply' || $post_type == 'topic' ) {
        if ( $user_can_view == 0 ) {
            $post_ok = false;
        } else {
            $post_ok = true;
        }
    }
 
    return $post_ok; 
}

Leave a Reply

Your email address will not be published. Required fields are marked *