Knowledge Base

Restricting results by user Groups using the Groups plugin by itthinx

in Ajax Search Pro Tips Tags: excludegroupsrestrictresultsusers

To restrict the plugin search results by user groups, when using the Groups plugin, please use the custom code below.

Add this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

add_filter('asp_results', 'asp_fix_groups_exclusions', 10, 1);
function asp_fix_groups_exclusions($results) {
	// Group based exclusions
	if ( class_exists('Groups_Post_Access') ) {
		foreach ($results as $k => &$r) {
			if ( isset($r->post_type) && !Groups_Post_Access::user_can_read_post($r->id) )
				unset($results[$k]);
		}
	}
	// Category based exclusions
	if ( class_exists('Groups_Restrict_Categories') ) {
		foreach ($results as $k => &$r) {
			if ( isset($r->post_type) && !Groups_Restrict_Categories::user_can_read($r->id) )
				unset($results[$k]);
		}
	}
	return $results;
}

Please note, that this may affect the number of results returned negatively, as this code will remove the unwanted results from the list during the results post processing.