Hi I need some info on how to achieve the below senario.
- I have a post type called "PRO".
- I have a custom taxonomy for above post type called "Pro Categories".
- Admin will create users in the backend with a custom role called "project"
- Admin will create custom taxonomy term and while creating he must choose a user with the above role (So only the list of users with the above role must be visible to choose)
- In the front end, when the user logs in, he must see (or have access to) only taxonomies / posts that his profile has access to. There is no backend access to users. Just front end restriction
Can someone here guide me the best possible way to achieve this please.
So far what i achieved.
- Created user role using user role editor
- Using ACF pro, user relation field, i am displaying all the users related to "project" in taxonomy section.
- Created Two taxonomies and two users and tagged them to check the results
Below is the code which checks wether current user has access to taxonomy tagged or not.
$current_user = wp_get_current_user();
if(in_array('project',$current_user->roles)){
$pro_cats = get_terms('pro-category','hide_empty=0');
//print_r( $pro_cats);
$allowed_cats = array();
if(is_array($pro_cats) && count($pro_cats)){
foreach($pro_cats as $pro_cat){
$auth_users = get_field('authorized_users', 'pro-category_'.$pro_cat->term_id);
if(is_array($auth_users) && count($auth_users)){
foreach($auth_users as $auth_user){
if($auth_user['ID'] == $current_user->ID){
$allowed_cats[] = $pro_cat;
$link = get_bloginfo( 'url' ) . '/pro-category/' . $pro_cat->slug . '/';
echo 'Name - '.$pro_cat->name.' - link -'.$link.'<br />';
//print_r( $pro_cat->name);
//print_r( $pro_cat->slug);
}
}
}
}
}
}
So from the above, when user logs in, i can see what taxonomy he is tagged in.
Now, I wanted to put restriction on taxonomy archive / single posts. So when a user is logged in, he must access only taxonomies related to his profile.