I am writing a plugin which grabs an array of data from the wordpress database using…
$data = $wpdb->get_results("SELECT * FROM $wpdb->users", ARRAY_A);
This works fine and I can display all the info from the users table, the issue I have is that I need to also pull out the First name and Last name which are in the wp_usermeta table.
Is there a way to modify the statement to also pull those details from the other table?
Not in one query and not necessarily the slickest either, but the following will produce what you want:
EDIT: Here it is in one query:
Note that opposed to the first version, the latter does not produce
$data[x]['first_name']and$data[x]['last_name'], but$data[x]['name']instead. This is due to either being stored in the “meta_value” column. It is not possible to accomplish your task in one query and store the first and last name in two different array keys at the same time.Hence, if doing it the second way, you’d have to use php’s
explode()function later on to access the name. Or correct it in a loop after the query has been run: