In what part of the WordPress core does the users table and usermeta table get joined?

I have been trying to work out just where in the massive jungle of WordPress include classes the usermeta table is joined onto the users table and if so, how does it work?

The one confusing thing about the usermeta table to me is that it is using key/value fields for the database fields and not actual values like first_name or last_name. How does WordPress know which fields to pull out by default and are there hooks, actions and filters for adding and retrieving data from the usermeta field?

Related posts

Leave a Reply

1 comment

  1. I’m not 100% what you are asking, it seems like several questions? But here goes:

     $meta_value = get_user_meta($user_id, $key, $single);
    

    For example:

     $first_name = get_user_meta($user_id, 'first_name', true);
    

    As for adding hooks I think this answer might be what you are looking for?

    UPDATE

    Based on some follow up comments I’ll add:

    1. The function get_user_metavalues($user_ids) from /wp-includes/user.php will return an array of user data arrays.

    2. The function get_userdata() retrieves user values using get_user_metavalues($user_ids) from /wp-includes/pluggable.php and returns a user data object.

    3. User Meta is managed through the generic metadata functions found in /wp-includes/meta.php so if you are looking for a SQL JOIN between wp_users and wp_usermeta you are not likely to find one. That file includes these functions:

    add_metadata($meta_type,$object_id,$meta_key,$meta_value,$unique=false)
    update_metadata($meta_type,$object_id,$meta_key,$meta_value,$prev_value='')
    delete_metadata($meta_type,$object_id,$meta_key,$meta_value='',$delete_all=false)
    get_metadata($meta_type,$object_id,$meta_key='',$single=false)
    update_meta_cache($meta_type,$object_ids)