How to allow users in wordpress to switch between two assigned roles e.g. Candidate & Employer

Lets say you are logged in as “Candidate” and if you want to switch to “Employer” , you should then create a new user record with employer role and keep the candidate user_id in employer usermeta table as “switch_user_id”

Suppose that candidate user_id is 4 and if you switch it will first check if “switch_user_id” exist or not, if not then it will create a new user with employer role. For this this newly created employer you should also store “switch_user_id”.

So now if you try to switch then get the switch_user_id , logout the current user and login to the other user id.

wp_users

id |         user_email                | username ... 
4  |      [email protected]          | candidate 
5  | [email protected] | candidate_gmail_com

wp_usermeta

user_id |     meta_key   | meta_value
   4    | switch_user_id |   5
   5    | switch_user_id |   4

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)