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 7940733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:27:57+00:00 2026-06-03T23:27:57+00:00

I have a query that is meant to return user details from WordPress tables.

  • 0

I have a query that is meant to return user details from WordPress tables. It would be fine if I was just SELECTing column names that I want, but within WordPress there is a usermeta table which has 2 columns – 1 called metakey and 1 called meta value.

I want to get certain bits of user info from meta keys such as first_name and last_name, but they are all within the same column – metavalue.

Here is what I have:

    $allquery="SELECT $comma_separated, wp_usermeta.meta_value, wp_usermeta.meta_key, 
//comma_seperated are a list of values to search for seperate by ",". this is name, email
    GROUP_CONCAT(
        wp_usermeta.meta_value
        ORDER BY wp_usermeta.meta_key
    ) AS name
    FROM wp_users
    LEFT JOIN wp_usermeta
    ON wp_users.ID = wp_usermeta.user_id
    WHERE  (wp_usermeta.meta_key = 'first_name'
        OR wp_usermeta.meta_key = 'last_name')
        AND wp_users.user_login = '$spec_user'
    GROUP BY wp_users.ID";

    $names = array();
    $allresult=mysql_query($allquery) or die(mysql_error()); 

    while($rows=mysql_fetch_array($allresult)){  

        $names[] = $rows['name']; //name is from the group_concat in query
        $emails[] = $rows['user_email']; 
    }

The problem is, this just returns a single row. I think it could be something to do with the line:

AND wp_users.user_login = '$spec_user'

Where $spec_user is a user entered value.

What I want ideally is all rows returned where the user_login column equals what the user has entered. Not just from that table, but from the usermeta table.

EDIT
@nnichols, I probably didnt go about explaining it properely…

This is the user table:

ID    userlogin    password    email        date-registered
2     jay          xxxxxx      xxx@xxx.xcom 1/2/12
5     pete         xxxxxx      xxxx@xxx.com 2/2/12

And this is the usermeta table:

umetaid     userid    meta_key     meta_value
122         2         first_name   james
123         5         first_name   peter
155         2         last_name    jones
167         5         last_name    gould
168         2         DOB          16/8/89
190         5         DOB          23/5/70

So I am trying to acheive pulling the values in a single query into a PHP loop so I can add them to an array like so:

$names = array();
$dob = array();

$allresult=mysql_query($allquery) or die(mysql_error()); 

while($rows=mysql_fetch_array($allresult)){  

    $names[] = $rows['name']; //name is from the group_concat in query
    $emails[] = $rows['user_email']; 
    $dob[] = $rows['custom_field_dob']; 

}

I have the first and last name already put into one array, names[]. But how would I acheive it so essentially the usermeta table treated the DOB metakey for example, as a column, so I could pull all content to the array, and be in the samme “row” as the other user data.

  • 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-06-03T23:27:58+00:00Added an answer on June 3, 2026 at 11:27 pm

    You can use the following sql to achieve your goal, may need to change table name, field name etc……

    SELECT distinct(user.id),

    (SELECT GROUP_CONCAT(u2.meta_value SEPARATOR ' ') FROM usermeta as u2 where u2.user_id=user.id and (u2.meta_key='first_name' OR u2.meta_key='last_name')) AS name,

    user.email as user_email,(SELECT u3.meta_value from usermeta u3 where u3.user_id=user.id and u3.meta_key='DOB') AS custom_field_dob

    FROM user, usermeta WHERE usermeta.user_id = user.id

    See the image below…
    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that looks a bit like this: SELECT weekEnd, MAX(timeMonday) FROM
I am using in C# MYsql .I have query that works if I run
I have a query that looks like SELECT P.Column1, P.Column2, P.Column3, ... ( SELECT
I have a query that I know can be done using a subselect, but
I have a query that updates one record, and only one record. Is there
I have a query that might be executed by several users consecutively. I'm scared
I have a query that shows me a listing of ALL opportunities in one
I have a query that uses a where clause. At times, this may be
I have a query that returns a result set similar to the one below:
I have a query that returns a string, as well as an escape character

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.