I have created a custom registration page, whereby a person will receive a code from another user and type in a code upon registering. If the code matches, i need the user who has just registered to become related to the user who they received the code from. Note i am saving these codes in a custom plugin table.

So you will have a one to many relationship, where a user can have many sub users. If possible i need to be able to extend that further so that a sub user could also have many sub users. So the first relationship and my priority right now would be:


I would then like to be able to do this:

SUB-USER B  -------
    |             |

User B is related to User A. User C and D are related to B.

I know we can make use of the and Update User Meta and query these relationships with the following query, i would just like some advice about creating this sort of relationship going forward.

$users_sub_users = new WP_User_Query( array(
   'meta_key' => 'user',
   'meta_value' => $user_id
) );
  • I wonder why, going in, you want to establish or imply a hierarchy. Seems to me that each user would get a user_meta array whose elements would probably just be other user_id's. Advising you any further would require knowing a little to a lot more about what uses you anticipate making of the data. – CK MacLeod Jul 21 '18 at 16:19

if I were you, I would use the wp_usermeta table. it is possible to add information to this table easily:

add_user_meta(NEW_USER_ID, "PARENT_USER_ID",USER_ID,true);

USER_ID: the Id of top level user, (In your case id of user A) "PARENT_USER_ID": some key that you could search and find result based on NEW_USER_ID: The Id of sub_user true: To let many user be sub user of the same user

for instance if USER A id is 41, and the Id of new user (which is B) is 82 your code look like:

add_user_meta(82, "PARENT_USER_ID",41,true);

then you can search for all users which their PARENT_USER_ID is user_id (in our example 41) and then you will get all sub users for that user.

sample query:

$user_query = new WP_User_Query( array( 'meta_key' => 'PARENT_USER_ID', 'meta_value' => '41' ) );
  • Thanks for that ill try this out! To clarify, i am using a plugin to store these codes because there is other information they would like in a separate table/view and there will be other variables that will be displayed here. Would i be able to make use of these update_user_meta functions within the plugin? – zen_1991 Jul 21 '18 at 8:09
  • Ok, you can use your preferred plugin, however this is a simple easy way to make relation between your user, you just need to add an action, to add this user_metakey, to one method of plugin which create user. – Arash Rabiee Jul 21 '18 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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