Exclude Restricted Forums from bbPress Main Query

By default, when you restrict a bbPress forum with Paid Member Subscriptions, users will still see this forum on the `/forums` page but if they try to access it, they will be prompted with a message or redirected to another page indicating that they cannot access that content.
It is also possible to completely remove from view these forums.

If you add the custom code from below to your website, the `/forums` page will only display forums that the current user can access:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Remove restricted forums from the main bbPress query
add_filter( 'bbp_after_has_forums_parse_args', 'pmsc_exclude_restricted_forums_from_main_query' );
function pmsc_exclude_restricted_forums_from_main_query( $vars ) {
	if( !function_exists( 'pms_is_post_restricted' )) return;
 
	$query = new WP_Query( $vars );
 
	$forum_ids = array();
 
	foreach ( $query->posts as $forum ) {
		$forum_ids[] = $forum->ID;
	}
 
	$restricted_forums = array_filter($forum_ids, 'pms_is_post_restricted');
 
	$vars['post__not_in'] = $restricted_forums;
 
	return $vars;
}

You can also download the code directly as an add-on from the following link:

Download Exclude Restricted Forums from bbPress Query

After downloading, you need to install this like you would install any other plugin.

If you don’t know how to use these code snippets, please read the instructions found at the top of this page.