Global Search questions
Hi, first, FC Extensions is pretty awesome. Lots of enhancements to FC that look pretty cool.
I'm looking at using Global Search in place of the custom search I built for my site but have a couple questions.
-
We use the Members plugin to assign WordPress roles to regular WordPress pages. Is there a way to incorporate these permissions into the search results so pages that the user has no access to won't be returned in the search results.
-
We have an ACF custom field on regular WordPress pages titled 'Hide from search'. These are typically administrative type pages that nobody would ever need to search for. Is there a way or maybe a filter available to exclude these from the search results?
Thanks!!!!
Global Search already takes care of who sees what. It only shows published content, and in the community each member finds only the spaces, courses and posts they have access to.
Your two cases are specific to your site, and here is how to set them up.
1. Pages restricted with Members
Global Search is open to any membership plugin. Add these few lines, and the results follow your Members rules:
add_filter( 'fcgs_wp_can_see', function ( $can_see, $post_id, $user_id ) {
if ( $can_see && function_exists( 'members_can_user_view_post' ) ) {
return members_can_user_view_post( $user_id, $post_id );
}
return $can_see;
}, 10, 3 );
- Members with the right role find the page.
- Other members and visitors don’t see it.
- Child pages follow the same rule.
- Administrators and editors see everything.
- When you change a role on a page, the results follow at once.
You can add the code in your child theme’s functions.php, or with a plugin like Code Snippets. It keeps working after every update.
2. Pages with “Hide from search” ticked
Great idea! We’re adding it to the next version of Global Search: a setting where you pick your ACF field, and every page where it is ticked stays out of the results. No code needed. We’ll let you know as soon as it’s out.
Before you start
- In Global search → Settings tab → Searchable contents → WordPress site, switch on Pages.
To check the result
- Global search → Relevance tab → Try a search, then type a member’s name in See as a member. You see exactly the results that member gets.
We tested the code with the current version of Members: members with the role, members without it, visitors, administrators and child pages.
Enjoy, and let me know how it goes!
Dragan Stamenkovic Thanks! I'll give that a shot. Appreciate it!
Thomas With pleasure. Please don't hesitate to ask or to flag anything if you notice something. Enjoy 👍