What it needs to be like:

  • one "super user" with user role company
  • normal users with user role employee

  1. I don't want "super user" to be able to have more capabilities, especially admin-like capabilities.

  2. I don't want to use custom post to emulate company because postmeta table is already getting too big compared to the rest. Also I need both to be able to login and that "super user" can manage its users in front-end.

  3. Normal users should be query-able by "super user" a.k.a query all users who are employees in that specific company.

How to make that kind of connection between user and "super user"? Obviously usermeta is the way to go but how to approach this? I don't need a huge code that works, I need an idea how it would work.

  • 1
    This strikes me as "too broad" and kinda "work for me for free".
    – s_ha_dum
    Dec 17, 2015 at 15:56
  • @s_ha_dum No, no.. I only need an idea how to make a connection between 2 user roles / users where 1 is superior and "acts as a group". I've searched for this solution for all these months I've developed my site but nothing: no plugins, no tutorials, not even search results in Google. I really don't want to use custom post.
    – N00b
    Dec 17, 2015 at 15:59
  • You have an idea-- usermeta. Users aren't built to "nest" like categories do, so what you are asking is "how do I alter the basic structure of WordPress Users/Roles". That is pretty broad.
    – s_ha_dum
    Dec 17, 2015 at 16:04
  • How would it look like? That every normal user have usermeta about super user ID and vice versa? Then use this ID to query? That's the problem, building up this logic is killing me. Coding it.. I can already handle that.
    – N00b
    Dec 17, 2015 at 16:10
  • Well that's the problem. It is a weird, complicated thing to do with many, many implications. You may even be better off with a dedicated table to handle the relationships, but again, many many implications some of which may not be immediately obvious.
    – s_ha_dum
    Dec 17, 2015 at 16:14

1 Answer 1



First of all you need to register the 2 roles, look at add_role.

When you register the role, you are free to assign any capability you want.

Only be careful to add the roles when your theme / plugin is activated and possibly remove them (see remove_role) when it is disabled.

Meta Data

You can do the association using a meta entry like company_employees that stores an array of user IDs. You can associate some custom function to get / set employees to a company user.

Get Employees of a Company

Example code for a function that return an array of employees given an user:

function getEmployees(\WP_User $user) {
  // if the user has not the role "company_role" can't have employees
  if ( ! in_array('company_role', $user->roles, true) ) {
    return array();
  $meta = get_user_meta($user->ID, 'company_employees', true);
  if (empty($meta)) {
    return array();

  $query = new WP_User_Query(array(
    'role'    => 'employee_role',
    'include' => (array) $meta

  return $query->results;

Assign Employee to a Company

In the same way you can write a function to assign an employee to a company user:

function assignToCompany(\WP_User $employee, \WP_User $company) {
  // if the company user has not the role "company_role" can't have employees
  if ( ! in_array('company_role', $company->roles, true) ) {
     return false;
  // if the employee user has not the role "employee_role" can't be assigned
  if ( ! in_array('employee_role', $employee->roles, true) ) {
     return false;
  // get current employees
  $employees = get_user_meta($company->ID, 'company_employees', true);
  is_empty($employees) and $employees = array();
  // add employee and update
  $employees[] = $employee->ID;
  $update = update_user_meta($company->ID, 'company_employees', $employees);

  return (int) $update > 0;


Of course, you need some sort of UI to assign employees to users. If an employee may have only one company, you can show a dropdown menu in the user edit page where you list all the companies and, on save, you can leverage the function above to store the employee in the company user meta.

To do that:

  • look at edit_user_profile hook to print the dropdown menu. Be sure to show the menu only to users that are allowed to edit the employee company
  • look at edit_user_profile_update hook to save the company in the meta field. Be sure to proceed with save only if current user is allowed to edit the employee company
  • be sure to also add a nonce field when you print the dropdown menu, and verify it before proceed with save. See wp_nonce_field() and wp_verify_nonce()

Final Touches

Company User Admin UI

You can add the list of the employees currently assigned to a company user in the user edit page.

Make it editable is a bonus.

Utility functions

You can add a couple of utility functions like isEmployee(), isCompany() that accept an user object ot id and return a true / false if the user has the related role.

isEmployee might also accept an optional param with a company user, and return true only if the user is an employee of that specific company.

Custom Capability

It may be a good idea to introduce a custom capability 'assign_employee' that you can assign to administrators, and to any other role you want to be able to assign employees users to company users. This could be useful when you need to show rthe company dropdown and when you have to save the company meta.

With this capability in place you can check if an user is allowed to do that using current_user_can('assign_employee').

To assign a custom capability to an existent role, have a look to WP_Roles::add_cap().

If you decide to do this, remember to remove the capability when the plugin / theme is disabled.

Frontend Company Page

Don't know if you need it, but you can create a "company page" in frontend, using a custom page template and a custom endpoint to show the user company description and a list of all employees


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.