Forums › Forums › Search & Filter Pro › Filter results based on user-role logged in
Tagged: filter, query, User roles, Wordpress loop
- This topic has 12 replies, 2 voices, and was last updated 6 years, 9 months ago by Anonymous.
-
Anonymous(Private) January 18, 2018 at 10:18 am #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 teamAnonymous(Private) January 18, 2018 at 2:08 pm #153272I 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 thetax_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.
Anonymous(Private) January 18, 2018 at 2:41 pm #153283Thanks @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/
Anonymous(Private) January 22, 2018 at 1:59 pm #153908I’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(Private) January 22, 2018 at 6:09 pm #153961Hi
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? -
AuthorPosts