Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6837323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:27:48+00:00 2026-05-26T23:27:48+00:00

I am writing a plugin which grabs an array of data from the wordpress

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:27:48+00:00Added an answer on May 26, 2026 at 11:27 pm

    Not in one query and not necessarily the slickest either, but the following will produce what you want:

    $data = $wpdb->get_results("SELECT * FROM $wpdb->users", ARRAY_A);  
    $i = 0;
    foreach($data as $single) {
        $meta = $wpdb->get_results(
            "SELECT meta_value
            FROM $wpdb->usermeta
            WHERE user_id = $single[ID]
            AND (meta_key = 'first_name' OR meta_key = 'last_name')
            ORDER BY meta_key",
            ARRAY_A
        );
        $data[$i]['first_name'] = $meta[0]['meta_value'];
        $data[$i]['last_name'] = $meta[1]['meta_value'];
        $i++;
    }
    

    EDIT: Here it is in one query:

    $data = $wpdb->get_results(
        "SELECT $wpdb->users.*,
            GROUP_CONCAT(
                $wpdb->usermeta.meta_value
                ORDER BY $wpdb->usermeta.meta_key
                SEPARATOR ' '
            ) AS name
        FROM $wpdb->users
        LEFT JOIN $wpdb->usermeta
        ON $wpdb->users.ID = $wpdb->usermeta.user_id
        WHERE ($wpdb->usermeta.meta_key = 'first_name'
            OR $wpdb->usermeta.meta_key = 'last_name')
        GROUP BY $wpdb->users.ID",
        ARRAY_A
    );
    

    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:

    $i = 0;
    foreach($data as $single) {
        $name_parts = explode(' ', $single['name']);
        $data[$i]['first_name'] = $name_parts[0];
        $data[$i]['last_name'] = $name_parts[1];
        $i++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Wordpress plugin which injects a grid of images just above the
I am writing a WordPress plugin which will include default text (i.e. a text
I am writing one plugin using NPAPI. I was looking for simple example which
I am writing a vim plugin in which i need to determine all those
I'm writing an app for gnome which will support plugins. Each plugin will contain
I'm writing a plug-in for WordPress which in fact will be a separate ordering
I am writing an app. There are plugins which DL resource from the net.
I'm writing a plugin which creates a custom post_type called dictionary_entry which has several
I'm writing a plugin for my wordpress site and am having trouble understanding the
I am writing an eclipse plugin which needs to be able to determine which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.