Skip Navigation

[Resolved] Filtering Listings Based on User Role

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 9 replies, has 1 voice.

Last updated by Minesh 1 week, 2 days ago.

Assisted by: Minesh.

Author
Posts
#2801309

Hi Toolset Support,

I have a setup where listings are displayed on the site, and I need to filter them based on the user role. Specifically, I want to ensure that only users with the role "Broker" have their listings displayed. If a user is not assigned this role, their listings should not be visible on the front end.

Is there a way to achieve this using Toolset? If so, what would be the best approach to implement this?

Thanks in advance for your help!

Best regards,

Sasa Vidakovic
Deal Studio

#2801352

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Have you checked the access control for post types.

More info:
- https://toolset.com/course-lesson/setting-access-control/
- https://toolset.com/course-lesson/restricting-access-to-pages/

You can follow any of the course we offer that has the above feature:
- https://toolset.com/course/

#2801400

Hi Minesh,

Thanks for your prompt reply.

Can you please help me understand how should I filter listings on this public page with Access if I need to showcase only listings from the wordpress users in the role Broker, and not show if they are set to Subscriber role for example:
hidden link

Same will applies for listing content template:
hidden link

Thanks in advance,

Sasa Vidakovic
Deal Studio

#2801535

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Have you try to check with post group:
- https://toolset.com/course-lesson/restricting-access-to-pages/#create-a-post-group-for-pages-restricted-to-logged-in-users

Try to create a Access post group and add your page to the post group and grant access to your desired role.

When you say:
Same will applies for listing content template:
hidden link
===>
Do you mean that all posts belongs to post type "listing"?

#2801543

Hi Minesh,

Thanks for your reply.

What I mean is that WordPress users who belongs to the broker role are allowed to post their own listings, trough designated private dashboard, which was achieved with Access plugin. Sometimes those brokers retire or they do not pay their yearly subscription and they are moved from broker to subscriber role. Subscriber role doesn't allow them to add listings from their designated dashboard, as I said which is achieved with Access plugin.

Now, what I would love to do, if it is possible, when we downgrade them from broker to subscriber role, that listings published from those brokers, now subscribers, are not anymore visible on the website. I would love to do some kind of conditional logic where I can say "do not show listings from user accounts that do not belong to broker role".

Let me know if you need any further explanation.

Best,

Sasa Vidakovic
Deal Studio

#2801631

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I would like to know how you are swiching from Broker role to subscriber role?
- Are you doing this manually?
- Are you using any subscription plugin?

#2801632

Hi Minesh,

Manually, the office assistant is managing that manually.

Best,

#2801665

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

There is no such native feature available with Toolset.

In such a case you should try to use the "set_user_role" hook:

add_action('set_user_role','func_update_post_status_after_user_role_update_change',10,3);
function func_update_post_status_after_user_role_update_change($user_id,$role,$old_roles) {
    if($role == 'subscriber' and in_array('broker',$old_roles) ) { // or whatever you want
        $posts = get_posts('numberposts=-1&author='.$user_id);
        foreach($posts as $post) {
            /// do what ever you want either delete post or change post status
        }
    }
}

More info:
=> hidden link

This will reuqire pure custom code which is beyond the scope of our support policy. if you require any help with such custom code, you are welcome to contact any of our certified partners:
- https://toolset.com/contractors/

#2801692

Thanks Minesh,

I appreciate all your replies and help.

Just one more clarification, so I can't set rule in the current view (hidden link) that is showcasing only Listings that are belonging to the user role Broker?

Best,
Sasa Vidakovic
Deal Studio

#2802013

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

If you are talking about the post type view, you can filter the view result with the post author:
- https://toolset.com/course-lesson/filtering-custom-lists-of-posts/#filtering-by-author

If you are talking about the user kind view, then you can set the source of the users based on the role.

If you want to filter post type view result based on xyz role then the workaround would be, you will have to add the following filter hook to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

function func_filter_view_basedon_role( $query_args ,$view_settings, $view_id ) {
    global $WP_Views;
        
    if ( $view_id == 999999) {
        
     $args1 = array( 'fields'=>'ID',
                                'role' => $WP_Views->view_shortcode_attributes[0]['role'],
                                'orderby' => 'user_nicename',
                                'order' => 'ASC'
                            );
      $found_user_ids = get_users($args1);
       $query_args['author__in'] = (!empty($found_user_ids))? $found_user_ids:array(0);
         
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_filter_view_basedon_role', 99, 3);

Where:
- Replace 99999 with your original view Id.

To control the view's shortcode argument, you will have to enable the legacy version of view:
- https://toolset.com/course-lesson/enabling-legacy-version-of-toolset-views/

And you can call the above view as with your desired role slug as given under:

[wpv-view name="postype-view-slug" role="author"]

Where:
- you can adjust the view's shortcode attribute "role" value as required, currently its set to display "author" role.

More info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
-