WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need help. I need to get a user ID or email (email is can do but ID is better) based on their phone number, outside of WordPress. I already have their phone number in a variable and billing_phone is custom metadata in the wp_users table. This is what I am using to load wp

`$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] ); require_once( $parse_uri[0] . 'wp-load.php' );`    

and how I am fetching the email.

    `$email = $wpdb->get_var("SELECT user_email FROM wp_users WHERE billing_phone = $phone_number ");`

Thank you all in advance. Any help is appreciated.

share|improve this question
Locating wp-load.php that way looks like you are loading it from within a plugin. This is generally not a good idea. What are you trying to do? – Steve Buzonas Feb 19 '13 at 20:12
There is no meta data in wp_users table. And you don't need wp-load.php to query the database. Use plain mySQL query from your external script. – Max Yudin Feb 19 '13 at 20:20
@Steve I was planning to put it in a plugin at first that is why I load it that way. – J. F. Bond Feb 19 '13 at 20:29
@Max lol I just noticed that i was doing that. My brain is fried. Thank for pointing that out. – J. F. Bond Feb 19 '13 at 20:32
If you are placing it in a plugin for wordpress why are you acting like it isn't within wordpress? – Steve Buzonas Feb 19 '13 at 20:44
up vote 0 down vote accepted

If you have billing_phone as user meta, which is the preferred way, that query would be incorrect. The advantage of utilizing custom user meta and adding a field for billing_phone would allow you to use get_users().

$user = get_users('meta_key' => 'billing_phone', 'meta_value' => $phone_number, 'fields' => 'ID');

The above would give you an array of User IDs that have the matching billing_phone meta value.

share|improve this answer
I've tried using get_users in a few different ways and it never works. The only wp function I can get to work is email_exists. I am soooo stuck on this. – J. F. Bond Feb 19 '13 at 20:58
Based on your query in the question I wouldn't expect get_users to work. Get users function calling a meta key and value will only work if the user meta is registered the wordpress way and appears within the usermeta table. – Steve Buzonas Feb 19 '13 at 21:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.