Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Filter results based on user-role logged in

Viewing 10 posts - 1 through 10 (of 13 total)
  • Mitchel van Eijgen
    #153195

    Hi!

    We’ve got a client who needs a very specific feature. We couldn’t find it straight away, so perhaps you can help us.

    We’ve got a website with a posttype which logged in users can access the posts. Currently every user has the same access to all the posts. Now our client needs a new user role, which can only access certain terms in a taxonomy.

    So as an example:
    User 1 can see all terms. (fruit, vegetables, nuts)
    User 2 can only see the fruits and vegetables.

    The only solution we could think of, is to create a new s&f filter for each user-role. (It will be more than 1, and more than 1 posttype, so a lot of work to manage)

    It would be nice to use the default meta_query function of the WordPress loop, to pre-filter the results in the filter. That way we could only filter through the results our user roles are allowed to see.

    Thanks in advance!

    Kind regards,
    Mitchel and team

    Trevor Moderator
    #153260

    Have you created some different user roles, applied these to some posts, rebuilt the cache, to see if it works as expected? It should from the same form.

    Mitchel van Eijgen
    #153262

    What do you mean with

    > applied these to some posts

    There isn’t really a way to apply user roles to a post right? And if you could do that, how would you than check which post to load based on the user (role) that us currently logged in?

    Trevor Moderator
    #153266

    There are plugins that restrict certain posts to certain user roles.

    Mitchel van Eijgen
    #153272

    I know there are, but I would figure that they don’t really play nice with S&F (and that part isn’t really the problem, because I know how to fix that myself), but if I would fix that the loop on the page would just show all my posts (even the once that are not allowed for that specific user (role) and then when they click the post they would be prompted with a notice that they where not allowed to view this single post page.

    A way to fix this would be to create a S&F for each user role that only shows the posts that they would be allowed to see. If I could just use the normal WP_Query I could programatically load in code that shows or hide certain terms using the tax_query, but because S&F completely takes over the loop this isn’t really possible the (normal) WordPress way, so thats my question:

    How to have multiple queries on a S&F loop based on the current user (role) without creating an new S&F for each user role.

    Trevor Moderator
    #153278

    I have a feeling that another user recently posted an example of the code they used to do just this. I did search for it, but I could not find it. You can always add to the query and edit the query arguments using our filters:

    https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments

    Mitchel van Eijgen
    #153283

    Thanks @Trevor this seems like something that would be useful. Did a search myself because I was interested in the post did you mean this one? https://support.searchandfilter.com/forums/topic/filter-content-by-current-user/

    Trevor Moderator
    #153285

    Ah, that was it.

    Mitchel van Eijgen
    #153908

    I’ve managed to set new arguments for the query of my filter. Only now my filter doesn’t work anymore. The initial results don’t show my query argument’s rules, but I can’t filter results, nor load more results. I’m using infinite scroll, and the same results as the initial are loaded when I reach the end of the initial results.

    These are my query arguments (Anonymised for reference)

    Am I doing something wrong?

    //Libary filter for usertype1
    if (in_array( 'usertype1', (array) wp_get_current_user()->roles )) {
      if($sfid==123)
      {
        $query_args = array(
          'meta_query' => array(
            array(
              'key'     => 'roleaccess',
              'value'   => 'usertype1',
              'compare' => '!='
            )
          ),
          'tax_query' => array(
            array(
              'taxonomy' => 'librarycat',
              'field' => 'slug',
              'terms' => array('product-image', 'protocol', 'product-presentation'),
              'operator' => 'NOT IN'
            )
          )
        );
      }
    }
    return $query_args;
    
    Trevor Moderator
    #153961

    Hi

    I edited your last post to give it code ticks. You can only put one before and one after.

    I can’t see anything wrong. If, before the if($sfid==123) line you print out a div with a hello in it, and then use print_r inside pre tags, do you see the hello and the array before you modify it?

Viewing 10 posts - 1 through 10 (of 13 total)

The topic ‘Filter results based on user-role logged in’ is closed to new replies.