Skip Navigation

[Resolved] Content restricted by Post Groups still showing in view

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

Problem:
Content restricted by Post Groups still showing in view - hide or remove it when displaying with view

Solution:
You can use views "wpv_filter_query" and check against the current user roles and roles assigned with the post group.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/content-restricted-by-post-groups-still-showing-in-view/#post-1163812

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 5 years, 11 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
- 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 19 replies, has 2 voices.

Last updated by Ian Henderson 5 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1162776

If you visit this page: hidden link

The posts under 'Responses form the Community' are child posts of the original post, which have been added through CRED forms. The first reply is part of the post group 'Private' and should not be visible to the general public. If you view its direct link hidden link then it is restricted.

However, the view is not respecting the access rules, and is showing the post despite it being restricted. How can I get the views on this site to respect the rules imposed by Access with respect to Post Groups?

#1162844

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - Toolset stores the assigned "Post Group" value to the post meta key "_wpcf_access_group" in the post meta table.

Now, this is a known limitation that views output do not respect the "Post Group" assigned to posts which you are displaying using view.

Could you please tell me your exact requirements. You do not want to display the post which have been assigned private post group - correct?

#1162849

Hi Minesh

Thanks for your reply. What I need is straightforward - that the View respect Post Group rules. So, if a post is hidden for guest access, that it does not show in the view results to unregistered users, but that it does show to registered users.

So in my case I have a tick box field for 'private' which then assigns a post to the 'private' post group. I run a kind of membership site with 3 levels of members - if a post is assigned to the private group I would like only those registered users to see the post, and not guests.

So on this page: hidden link the first post 'Prueba 2' (it's just demo nonsense content) should not show to non-registered users, but should show to registered users.

So in basic form I'd like the View output to respect the rules of the post groups. Is there a way to achieve this?

#1162850

It's particularly important because here hidden link the first 'response' post is a private post (in the post group) but the view is showing the post in full to unregistered users, because it ignores the post group rules.

#1162879

Minesh
Supporter

Languages: English (English )

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

Could you please check now. I've added the following code at Toolset's Custom Code section:
=> hidden link

add_filter( 'wpv_filter_query', 'remove_products_in_a_product_group', 10, 3 );
 function remove_products_in_a_product_group( $query_args, $view_settings, $view_id ) {
   
    if($view_id == 941) {
      
       $query_args['meta_query']['relation'] = "OR";
        $query_args['meta_query'][] =array(
            'key'     => '_wpcf_access_group',
            'compare' => 'NOT EXISTS',
            'value'=>''
                                           
            );
      
     }
    return $query_args;
}

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

I've also added the query filter for field "_wpcf_access_group" to "RESPONSES ALL" view:

Select items with field:
_wpcf_access_group is a string different from wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec

I can see now it's displaying the desired results - could you please confirm:
=> hidden link

#1162915

Thanks Minesh, but your code has hidden the relevant post for ALL users.

The posts in this post group 'Private' need to be hidden for all users except the authorised groups of users (as per the privileges in the post group).

For instance, this is the code I'm using to hide the post form from non-registered users, which lists the three user groups that should be able to see the post :

[wpv-conditional if="( '[wpv-current-user info="role"]' eq 'global_abs_community' ) OR ( '[wpv-current-user info="role"]' eq 'global_abs_moderator' ) OR ( '[wpv-current-user info="role"]' eq 'administrator' )"]
[cred_form form="create-a-response"]
[/wpv-conditional]

#1163005

Minesh
Supporter

Languages: English (English )

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

So, if you want to go role base - you can add same conditional statement and just replace the form shortcode with your views shortcode and remove the filter I've added and the custom code I've added.

#1163482

Hi Minesh

I'm a bit confused. The entire point of my support thread is to have the View deliver content based on the user role, conditional on whether a post is part of a post group. I can easily make the View hide the 'private' content because post group membership is set up a field in the post, so I can use a simple query filter for that. But then the post is hidden for all users, just like your filter.

Using shortcodes within the view does not work very well because they need to go in the loop template, and then the row is still generated and the design breaks down, because there will be empty rows.

best
Ian

#1163772

Hi Minesh

I'm looking at your code again - is there a way to modify it to add the condition of checking for role base? So the filter checks for the the private field or post group membership AND checks for user role?

So if the private field is ticked it filters out the post unless the user is one of the three groups?

thanks
Ian

#1163784

Minesh
Supporter

Languages: English (English )

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

Well - I've try to adjust the code as given under:

add_filter( 'wpv_filter_query', 'remove_products_in_a_product_group', 10, 3 );
 function remove_products_in_a_product_group( $query_args, $view_settings, $view_id ) {
   global $wpcf_access;
   global $current_user;
   
  // echo "<pre>";
  // print_r($wpcf_access);
    if($view_id == 941) {
      
      $current_user_role = $current_user->roles[0];
      
      $permited_roles = $wpcf_access->settings->types['wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec']['permissions']['read']['roles'];
      
  if(!in_array($current_user_role,$permited_roles)){ 
      
       $query_args['meta_query']['relation'] = "OR";
        $query_args['meta_query'][] =array(
            'key'     => '_wpcf_access_group',
            'compare' => 'NOT EXISTS',
            'value'=>''
                                           
            );
  }   
       // wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec
      }
    return $query_args;
}

As per your request - we are checking to which roles we give read permission but again it applied to the whole view not per post but this will give you idea how it works. There is no other solution as there is no such feature exists.

#1163788

Hi Minesh

Thanks - are you saying that it doesn't work? Surely this is something that is a common issue for many people? This means a big part of Access doesn't work, because the whole point of Toolset is to present posts through Views.

Is there no way around this?

Thanks

#1163812

Minesh
Supporter

Languages: English (English )

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

Ok - I think finally, following solution should work for you.

What we are doing is - we are fetching all the private posts IDs and also checking with the current user role, if current user role is available within the private post group then we it will exclude that post ID from the query.

add_filter( 'wpv_filter_query', 'remove_products_in_a_product_group', 10, 3 );
 function remove_products_in_a_product_group( $query_args, $view_settings, $view_id ) {
   global $wpcf_access;
   global $current_user;
  
    $permited_roles = $wpcf_access->settings->types['wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec']['permissions']['read']['roles'];
   
  $current_user_role = $current_user->roles[0]; 
   
   $private_ids = array();
  if(!in_array($current_user_role,$permited_roles)){ 
      
   $args = array(
        'post_type' => $query_args['post_type'],
        'meta_query' => array(
            array(
                'key' => '_wpcf_access_group',
                'value'    => 'wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec',
                ),
            ),
        'fields'    => 'ids',
        'posts_per_page' => -1,
    );
    
    $private_ids = get_posts($args);
  }
    if($view_id == 941) {
           $query_args['post__not_in'] = $private_ids;
     
       // wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec
      }
    return $query_args;
}

Could you please confirm above code works as per your expectations. Please test it and let me know how it goes.

#1163830

Hi Minesh

Yes, thank you this is working now. How do I add additional views to this custom code? Is it possible to list the views in some kind of array here?

if($view_id == 941) {
$query_args['post__not_in'] = $private_ids;

// wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec
}
return $query_args;

#1163833

I would like to have the same code apply to the following views:
1054, 1120, 1118, 1052, 846, 1011, 1093, 1472, 865, 1469, 1451, 1464, 1464 etc. - there is quite a long list.

#1163841

Minesh
Supporter

Languages: English (English )

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

Ok - I've changed the code as given under - you can add as many view IDs to array as required:

$check_view_ids = array(941,1054, 1120, 1118, 1052, 846, 1011, 1093, 1472, 865, 1469, 1451, 1464, 1464);
    if(in_array($view_id,$check_view_ids)) {
           $query_args['post__not_in'] = $private_ids;
     
       // wpcf-custom-group-2c17c6393771ee3048ae34d6b380c5ec
      }