0

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.

6
  • 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? Commented Feb 19, 2013 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
    Commented Feb 19, 2013 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
    Commented Feb 19, 2013 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
    Commented Feb 19, 2013 at 20:32
  • If you are placing it in a plugin for wordpress why are you acting like it isn't within wordpress? Commented Feb 19, 2013 at 20:44

1 Answer 1

0

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.

2
  • 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
    Commented Feb 19, 2013 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. Commented Feb 19, 2013 at 21:06

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.