• Hello,

    I’m wondering if someone could give some guidance..perhaps recommend a plugin.

    I need a multi layer search function for my site.

    I have pages (with posts) that are restricted to certain groups – this content has to be searchable within this particular restricted category

    Then I have pages & posts that are public, and the public search should not pull the restricted pages in results. I achieved this using the following code:

    [ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]

    add_filter( 'pre_get_posts', 'ja_search_filter' );
    /**
     * Exclude category 7 from search results.
     *
     * @since ?.?.?
     * @author Jared Atchison
     * @link https://gist.github.com/1300302
     *
     * @param WP_Query $query Existing query object
     * @return WP_Query Amended query object
     */
    function ja_search_filter( $query ) {
    
    	if ( $query->is_search && !is_admin() )
    		$query->set( 'cat','-100,-106' );
    
    	return $query;
    
    }

    However this prevents the private group to be able to search for the restricted content as well.

    Is there anyway to solve this? Comments need to be included in both searches as well.

    Thank you in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Identify a capability that only the private group has. Create and assign a custom capability if necessary. Then in your filter callback modify the !is_admin() logic to not restrict searches if the current user has this capability.

    Thread Starter kristina87

    (@kristina87)

    bcworkz,

    thank you for the response!

    I’m sorry I should have mentioned that I’m pretty new, could you give an example?

    also, could this work for multipe categories?

    I need one public search that will exclude private categories A B & C

    Then I need category A for example, so be searchable separately for users who’s role is set to view category A & only pull up the results form cat. A. they should not see results from cat. B or C

    same applies to cat B & C

    Moderator bcworkz

    (@bcworkz)

    The tricky part is assigning custom capabilities. Fortunately, there are a few plugins that let you easily manage that part, Members is one, there are others.

    Let’s say you assign all of these capabilities to admin so we do not need to deal with admin separately in the callback logic. Thus your pre_get_posts callback would have a series of if() statements, one for each group (or use a switch/case structure). One if statement might look like this:
    if ( $query->is_search && current_user_can('read_cat_a'))

    Also note that you can assign capabilities to both roles and individual users, so you can set up some very elaborate permissions.

    Thread Starter kristina87

    (@kristina87)

    Hi bcworkz,

    Thanks, it sort of worked but I must be doing something wrong

    I set a capability for “regular” which is the general public & is meant to exclude cat. 100 (designated for team A only) & 106 (for team B only)

    if ( $query->is_search && current_user_can('regular'))
    		$query->set( 'cat','-100,-106' );

    This worked. However then I created a capabilities for team A & team B
    & tried to repeat the the statement so that if current_user_can('teamA')) then it excludes cat 106 (which is for team B) – this failed. Search still pull results for cat 106.

    Any idea? Is it possible to layer it in such a way or am I simply doing it wrong?

    Thanks a lot for the help!
    Kristina

    Moderator bcworkz

    (@bcworkz)

    IIRC there may be an issue with uppercase letters in capabilities. The rules for naming post slugs applies here – all lowercase plain latin chars, no spaces, no special chars except hyphen – and underscore _.

    It’s definitely an issue somewhere in WP besides slugs, but I’m not entirely sure if this is it or not, give it a try anyway! If that fails, post your entire filter callback here or at pastebin.com, there could just be a hard to see typo or something.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘multi layer search’ is closed to new replies.