Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Filter search results based on authentication status
- This topic has 5 replies, 2 voices, and was last updated 5 years, 9 months ago by
Ernest Marcinko.
-
AuthorPosts
-
August 14, 2020 at 12:34 am #28928
philip29
ParticipantHello
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.
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!
August 14, 2020 at 9:17 am #28938Ernest Marcinko
KeymasterHi,
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.August 17, 2020 at 7:45 pm #29018philip29
ParticipantHe 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
August 18, 2020 at 10:03 am #29029Ernest Marcinko
KeymasterHi,
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; }August 18, 2020 at 11:36 pm #29040philip29
ParticipantThank 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.
August 19, 2020 at 9:12 am #29053Ernest Marcinko
KeymasterThat sounds like a perfect solutions, although I don’t know why the other one does not work, the code seems to be all right.
-
AuthorPosts
- You must be logged in to reply to this topic.