1

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?

1 Answer 1

2

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)
4
  • Hi Mike, thanks for answering. I just realised my question seems a bit erratic, I apologise for that. Basically all I want to know is how Wordpress internally populates the $userinfo (ci?) variable. More precisely how it joins the usermeta table to the users table and how it knows which fields to retrieve to combine into the one super user object. In which class file do the required queries take place to make this happen? Just wanting to get a little insight into how Wordpress operates. Jan 28, 2011 at 13:05
  • @Dewayne - I added an update. Let me know what else you need help understanding. Jan 28, 2011 at 13:31
  • Fantastic, thanks Mike. I like the loose approach that Wordpress takes in combining multiple pieces of data together through the use of a few functions. I guess it's better than hard wiring JOIN queries and whatnot. Jan 29, 2011 at 2:18
  • @Dewayne - Glad I could help. Jan 29, 2011 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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