I'm trying to show a category only for a specific user role in Wordpress. I found this bit of code which works because it isn't showing the category products when not logged in or when I have a different user role.
But the problem I'm having is as follow: The website is using WPML and my code worked only for the english language. But not for the other languages. So I added for testing another category id which is the same category but only this one is for the dutch language so I was expecting it to work for the english and dutch language but it just won't work expect for the english one.
The code I'm using now is:
function wholeseller_role_cat( $q ) {
// Get the current user
$current_user = wp_get_current_user();
// Displaying only "Wholesale" category products to "whole seller" user role
if ( in_array( 'wholeseller', $current_user->roles ) ) {
// Set here the ID for Wholesale category
$q->set( 'tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => '127,128', // your category ID
)
) );
// Displaying All products (except "Wholesale" category products)
// to all other users roles (except "wholeseller" user role)
// and to non logged user.
} else {
// Set here the ID for Wholesale category
$q->set( 'tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => '127,128', // your category ID
'operator' => 'NOT IN'
)
) );
}
}
add_action( 'woocommerce_product_query', 'wholeseller_role_cat' );
So the english category id is 127 and for the dutch language it's 128. Can someone help me to get this working again?
Hope anyone can help me with this?
Update
The English and Dutch languages are now only showing the category when user role is Wholeseller. But I've got some more languages on my website.
Here is the complete list with the corresponding category ID:
English (en) => 117
Dutch (nl) ===> 118
French (fr) ==> 131
Italian (it) => 134
Spanish (es) => 137
German (de) ==> 442
How can I make it work for more than 2 languages?