This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Filter search results based on authentication status

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Filter search results based on authentication status

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #28928
    philip29philip29
    Participant

    Hello

    I’m just wondering if it’s possible to remove search results containing restricted content if the person performing the search is not authenticated.

    I found this support article, which indicates yes, you can remove restricted content from search results. However we are using MemberPress, and have Apache Solr as our search backend.

    https://knowledgebase.ajaxsearchpro.com/miscellaneous/post-types/restricting-results-by-user-groups-using-the-groups-plugin-by-itthinx

    In my testing, our search results currently contain content from all pages/posts, regardless of authentication status. Can we use the code snippet from the link above to protect MemberPress-controlled content?

    Thanks!

    #28938
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    It should be possible, but a API method is required from the other plugin. The best would be a function that can check if the current user has access to a post by it’s ID.
    Let me know if there is any method in memberpress, and I can help you with the implementation.

    #29018
    philip29philip29
    Participant

    He Ernest

    MemberPress support suggested trying MeprRule::is_locked($r)

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

    This had no effect, all results were returned to all users. Do you see any issues with the code? This was placed in a child theme’s functions.php.

    Thanks

    #29029
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    That looks almost all right, but the $r variable in that case does not contain a post object, so this should be it:

    add_filter('asp_results', 'asp_fix_groups_exclusions', 10, 1);
    function asp_fix_groups_exclusions($results) {
    	// Group based exclusions
    	if ( class_exists('MeprRule') ) {
    		foreach ($results as $k => &$r) {
    			if ( isset($r->post_type) ) {
    				$post    = get_post( $r->id );
    				if ( MeprRule::is_locked($post) )
    					unset($results[$k]);
    			}
    		}
    	}
    	return $results;
    }
    #29040
    philip29philip29
    Participant

    Thank you, Ernest. Unfortunately it is still not working. I believe I’ve found a different way to address the issue: multiple ASP search forms, each with different constraints and conditionally displayed based on login status.

    #29053
    Ernest MarcinkoErnest Marcinko
    Keymaster

    That sounds like a perfect solutions, although I don’t know why the other one does not work, the code seems to be all right.

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