0

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.

3
  • Try to post a piece of code, to show what you tried, what you researched, so people won't downvote your question =) Commented Nov 8, 2017 at 13:44
  • @MarceloHenriquesCortez: Updated on what i have done so far. Thanks for letting me know. This is my first post here.
    – user124296
    Commented Nov 8, 2017 at 14:00
  • No problem =) I updated my answer. Let me now if it works, if there is any error msg, etc. Commented Nov 8, 2017 at 14:05

1 Answer 1

0

You should make a verification on the front-end to see if the user has the role 'pro 1', 'pro 2' or whatever.

Like:

$user = wp_get_current_user();
if (in_array( 'pro-1', $user->roles)):
//whats this user can see
elseif (in_array( 'pro-2', $user->roles)):
//whats this user can see
else :
endif;

If you are using a template, you can try adding this on the top of your code:

global $authordata; 
global $post;
setup_postdata( $authordata );
2
  • I Have only one custom role.. And i want to restrict taxonomy archive page, by checking if taxonomy is tagged to user (Acf relation field). If tagged, show, or esle redirect. Hope i am clear with this.
    – user124296
    Commented Nov 8, 2017 at 14:13
  • You need to check the user role in the front-end, so you need to use the code I posted. If you only have one role, just use the 'if' and don't use the 'elseif'. You don't need to 'link' things in the admin, just create the user role, create the custom taxonomy and on the taxonomy archive page, insert that verification. Commented Nov 8, 2017 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.