I am using the $wpdb->get_var php code to get the first name and last name on the records, in the wp_usermeta table... But because the field value is stored in the same cell, I don't know how to merge it into one string. Right now I do it like this:

$_EmployeeFName = $wpdb->get_var( $wpdb->prepare("SELECT `meta_value` FROM `wp_usermeta` WHERE `meta_key` = 'first_name' and `user_id` = %d ", $_REQUEST['empid']));
$_EmployeeLName = $wpdb->get_var( $wpdb->prepare("SELECT `meta_value` FROM `wp_usermeta` WHERE `meta_key` = 'last_name' and `user_id` = %d ", $_REQUEST['empid']));

Is there a way to do that in ONE pull from the database? I don't want to do this a lot, to save the database from so many pulls...

I don't know how to do it though.

do you?

Thanks, Richard

2 Answers 2


Why do your own database queries? Use the WP functions to do it for you.

$empid = (int) $_REQUEST['empid'];
$name = get_user_meta($empid, 'first_name', true) . ' ' . get_user_meta($empid, 'last_name', true);
  • That did it! I did not know that would work. :) I did not see it anywhere. Thank you!!
    – Richard
    Sep 28, 2014 at 9:38
  • Here is another question. In the form, I am putting a form tag and some hidden fields, then content. Wordpress is taking those html fields and making them put line breaks in, so there is a huge space between content. How can I remove the line breaks they cause so that they are not putting a lot of white blank lines on the form? Thank you again!!
    – Richard
    Sep 28, 2014 at 9:39

If you want to perform a single query that retrieves one field but for multiple results (e.g. to avoid round-trips to the database), the simplest explanation is to use get_results() instead.

Taking your query:

$wpdb->get_var( $wpdb->prepare("SELECT `meta_value` FROM `wp_usermeta` WHERE `meta_key` = 'first_name' and `user_id` = %d ", $_REQUEST['empid']))

... and seeing how it is nearly identical to the one of the other field:

$wpdb->get_var( $wpdb->prepare("SELECT `meta_value` FROM `wp_usermeta` WHERE `meta_key` = 'last_name' and `user_id` = %d ", $_REQUEST['empid']))

... I would suggest rolling them together into a single query:

$results = $wpdb->get_results($wpdb->prepare("
    SELECT `meta_key`, `meta_value`
        FROM `wp_usermeta`
        WHERE `meta_key` IN ('first_name', 'last_name')
            AND `user_id` = %d;", $_REQUEST['empid']));

Your $results object would be an array with meta_key and meta_value properties on each object.

You could then get your individual fields as follows:

foreach($results as $rec) {
    switch($rec->meta_key) {
        case "first_name":
            $_EmployeeFName = $rec->meta_value;
        case "last_name":
            $_EmployeeLName = $rec->meta_value;

Using get_results() allows you not only to query for both at the same time, but also to be able to differentiate between the two (providing both the meta_key and the corresponding meta_value).

  • ah, I see! That is how to do it in a single query, that is what I was looking for! Thank you!!
    – Richard
    Sep 28, 2014 at 9:45

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.