Skip Navigation

[Resolved] Hiding some categories for visitors

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 0 replies, has 1 voice.

Last updated by Dido 3 weeks, 1 day ago.

Assisted by: Minesh.

Author
Posts
#2787600

Hi,

on my site hidden link I have 4 products.
Only 1 product is for everyone, the rest are only availlable for members.

I have assigned the products to a member group, and that works. However, in the shop page they are not hidden.
How can I do this?

#2787784

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share admin access details as well as member access details and do you mean that on shop page those product should not be displayed?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2787874

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've added the following code to "Custom Code" section offered by Toolset with the code snippet namely "toolset-custom-code":
=> hidden link

add_action( 'pre_get_posts', 'func_remove_products_from_access_group_posts_archive_result',99,1);
function func_remove_products_from_access_group_posts_archive_result( $query ) {
    // skip if admin side or not main query
    if ( is_admin() || ! $query->is_main_query() ) return;
 
    // check if the query is for a specific post type
    if ( in_array ( $query->get('post_type'), array('product') ) ) {
        $user = wp_get_current_user();
		
   global $wpcf_access;
   global $current_user;
   
     //// get the post group slug belongs to toolset access assigned to post to check the permission
    $post_group_slug = 'wpcf-custom-group-edffc382fce896fb1838d71d60d9ed58';
	$permited_roles = $wpcf_access->settings->types[$post_group_slug]['permissions']['read']['roles'];
	
	// get current user role
    $current_user_role = $current_user->roles; 
  
   
  $private_ids = array();
  if(empty($current_user_role)){ 
       
   $args = array(
        'post_type' => 'product',
        'meta_query' => array(
            array(
                'key' => '_wpcf_access_group',
                'value'    => $post_group_slug,
                ),
            ),
        'fields'    => 'ids',
        'posts_per_page' => -1,
    );
	
	///// get posts where post group is assigned 
    $private_ids = get_posts($args);
	
	/// remove the posts from the view result belongs to post group
	$query->set('post__not_in',$private_ids);
      
  }
           
  }
}

I can see now only one product that is publically available and not assigned to access post group.

#2787875

Very well, thank you!