Skip Navigation

[Resolved] Different display limits in a View depending on User Role

This thread is resolved. Here is a description of the problem and solution.

Problem:
Can you limit items shown in the output of a View's Loop depending on who visits it?

Solution:
Not natively, but there is a easy custom solution explained here:
https://toolset.com/forums/topic/display-limits/#post-1296683

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 replies, has 2 voices.

Last updated by mikeH-3 5 years, 6 months ago.

Assisted by: Beda.

Author
Posts
#1296623

I am hoping you can help. I need to make the following happen in a View I made.

If logged in user role is set to 'free', than change the display limit of items to 4
If logged in user role is set to 'paid', than change the display limit of items to all

Is this possible? It seems I can only set the display limit for the view in general and not use conditions to set it.

Thanks!

#1296683

It's not possible natively in one View without using custom code.

The simplest is creating 2 Views, each paginated according to user roles' permissions, then insert both Views on the same page but wrap each in a Toolset Access (or HTML Conditional) ShortCode:
https://toolset.com/documentation/user-guides/access-control-texts-inside-page-content/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This allows you to show View A to user role A and View B to user role B.

If you want to do it in the same view, you'll need to filter the View's settings according to the current user.
You'd do this by adding your custom code which alters the Queries' max results settings, to the Views Filter here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_view_settings

There is an example there which already almost does what you need:

 //Modify the limit and/or offset settings of a given View to use a number greater than 50:
add_filter( 'wpv_view_settings', 'prefix_modify_filter_offset_view', 5, 2 ); // use a low priority number of 5, so this runs early
  
function prefix_modify_filter_offset_view( $view_settings, $view_id ) {
    if ( $view_id == 374 ) { // if displaying a View with ID equal to 374
        $view_settings['limit'] = 75; // show 75 posts
        $view_settings['offset'] = 60; // skip the first 60 posts
    }
    return $view_settings;
}

Now instead of only checking the View ID, you'd also check the current user role, this can be done with the WordPress API https://codex.wordpress.org/Function_Reference/current_user_can

Please let me know if you'd need more details on some step.

#1296933

My issue is resolved now. Thank you!