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

  • Home
  • SEARCH
  • 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 1075991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:20:59+00:00 2026-05-16T21:20:59+00:00

I am trying to create a single query for getting information from the main

  • 0

I am trying to create a single query for getting information from the main table (WordPress user table), and the user meta table.

If you’re not familiar with WP DB architecture: wp_users holds basic user information and their IDs, wp_usermeta holds the ID, meta_key, and meta_value.

Let’s say I want to get all users that have certain meta_keys and meta_values. This is the query I currently have (generated via PHP based on available meta_keys):

SELECT ID, 
user_email, 
user_login, 
first_name.meta_value as first_name,
last_name.meta_value as last_name,
phone_number.meta_value as phone_number,
wp_capabilities.meta_value as wp_capabilities 
FROM wp_users, 
(select * from wp_usermeta where meta_key = 'first_name') as first_name,
(select * from wp_usermeta where meta_key = 'last_name') as last_name,
(select * from wp_usermeta where meta_key = 'phone_number') as phone_number,
(select * from wp_usermeta where meta_key = 'wp_capabilities') as wp_capabilities 
WHERE  
ID = first_name.user_id AND 
ID = last_name.user_id AND 
ID = phone_number.user_id AND 
ID = wp_capabilities.user_id  AND 
wp_wpi_capabilities.meta_value LIKE '%administrator%'   
ORDER BY first_name

This does a good job, however if a certain user is missing a meta_key, ‘last_name’ for instance, that user row does not get returned at all. So really all I need is a way of returning a Null value for missing meta keys.

Right now I have a very hackish function that goes through and creates blank meta_keys and meta_values for all the users that don’t have a given meta_key, that way they are returned. This is a terrible way of doing it though when you have over a thousand users, and you need to add a new type of meta_key.

Let me know if anybody has done this, or if I need to explain better.

Thank you.

  • 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-16T21:20:59+00:00Added an answer on May 16, 2026 at 9:20 pm

    Put the usermeta joining criteria in the join rather than struggling with subqueries:

    SELECT
        ID, user_email, user_login, 
        first_name.meta_value as first_name,
        last_name.meta_value as last_name,
        phone_number.meta_value as phone_number,
        wp_capabilities.meta_value as wp_capabilities 
    FROM wp_users
        JOIN wp_usermeta AS wp_capabilities ON wp_capabilities.user_id=ID
            AND wp_capabilities.meta_key='wp_capabilities'
        LEFT JOIN wp_usermeta AS first_name ON first_name.user_id=ID
            AND first_name.meta_key='first_name'
        LEFT JOIN wp_usermeta AS last_name ON last_name.user_id=ID
            AND last_name.meta_key='last_name'
        LEFT JOIN wp_usermeta AS phone_number ON phone_number.user_id=ID
            AND phone_number.meta_key='phone_number'
    WHERE
        wp_capabilities.meta_value LIKE '%administrator%'
    ORDER BY
        first_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a single query that will pull results from a
I'm trying to create a single query containing optional aggregate values from multiple tables
I am trying to create a table with a single column using the following
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I'm trying to create a single query which, similar to stack overflow, will give
I am trying to create a database from a SQL query I've been sent.
I'm trying to create a SQL 2005 query to retrieve and combine records from
I'm trying to create a single query that will combine the following two queries.
I'm trying to create a simple address screen where the user has a single
I'm trying to create a single select listbox from Linq, however, I'm running into

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.