I have a project where I feel I need to create relationships between users but since this is not natively implemented in wordpress as far as am concerned then I will need advice on how to do it.

One user will have the custom role of agent, while another user will have the role of subscriber. I want to create a one-to-many relationship between agent and subscriber respectively. How to go about it is where I am stumped.

Ideally, when a subscriber logs in , they can select the agent that represents them from a dropdown in their profile, creating the relationship. This functionality will only for people with the role of subscriber. An agent can't create a subscriber. Agents are manually registered by wordpress admins, subscribers use the registration form on the front end of the site.

As for agents, the relationship allows them, amongst other things, to view a table of their subscribers (first name, last name, email, telephone,total value of investment etc). Agents can also only see their subscribers.

If I was creating a database from scratch, I would simply create one table for agents, another for users with an agent foreign key but wordpress seems to put all users in one db table (unless am wrong)

The question is, in the scenario above, what will be the best way to create the agent-subscriber relationship? I know the question might come across as vague and doesn't have code but I hope someone with experience of such a problem can give me an idea.


EDIT Following @janh2 answer below, I have been able to create a relationship between agent and subscriber. I am going to show the code so that it might help someone in the future.

Code for adding user role

function gtb_add_user_role(){

        __('Customer Agent'),
            'read' => true,
            'level_0' => true

code for adding subscriber custom fields

//add custom profile fields 
function gtb_add_subscriber_custom_fields($user_id){

code in subscriber_custom_fields.php (could not work until I made user_id global, don't know why but anyway....)

global $user_id;
$agent_selection = get_user_meta($user_id,'customer_agent_rep',true);
 <table class="form-table">
        <label for="subscriber_customer_agent"><?php _e('Your Agent');?></label>
            $args = array(
                'role__in' => array('customer_agent')
            $agents_query = new WP_User_Query($args);

            $customer_agents = $agents_query->get_results();
        <select name="subscriber_customer_agent" id="subscriber_customer_agent">
            <option value="0" <?php selected($agent_selection,"0");?> >None</option>
                foreach($customer_agents as $customer_agent):
                    $the_agent_number = $customer_agent->ID;
                    <option value="<?php echo $customer_agent->ID; ?>" 
<?php selected($agent_selection,$the_agent_number);?>  > <?php echo $the_agent_number." - ". $customer_agent->display_name; ?></option>

Saving and Updating user meta

 //save or update custom meta
function gtb_save_subscriber_meta($user_id){
        //check and update agent_rep_meta
        $agent_representative = get_user_meta($user_id,'customer_agent_rep',true);

Action hooks to initiate custom profile field and user meta when theme is setup/activated.Some actions hooks hook to the top and some to the bottom,I am yet to fully understand so some of these hooks might be unnecessary.More experts can be explain further.


function gtb_theme_setup(){
  //add user role
   //hook custom profile fields at the end of the page.
    add_action ('show_user_profile','gtb_add_subscriber_custom_fields',10,1);
//save or update  custom meta

 //code for enqueuing styles and scripts and other theme functions goes here.......


@Janh2 answer is more useful on the Agent side, you can create a table in the Agent's backend (view) where you can use the code to populate a table.

I hope this contribution is useful to someone. Many thanks :-)

1 Answer 1


WordPress does provide you with the ability to save meta data for users, so you could just store the agent's user id in a meta field of his subscribers, like update_user_meta( $user_id, "agent", $agent_id ).

You can then query those users with the WP_User_Query's meta_query, e.g.

$agents_subscribers = new WP_User_Query( array(
    'meta_key' => 'agent',
    'meta_value' => $agent_id
) );
  • Many thanks for the answer. I am going to do a test run of this, together with any other forthcoming answers over the weekend and will mark it on monday.
    – nelson
    Oct 13, 2017 at 13:31
  • Thanks for pointing me to the right direction. I have done a test and I was able to establish a relationship via user_meta. Also learned about the various hooks I need when user profile is being set up and activated.
    – nelson
    Oct 16, 2017 at 15: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.