WordPress.org

Ready to get started?Download WordPress

Forums

get_var email address query error (5 posts)

  1. griffgj
    Member
    Posted 1 year ago #

    I feel like I'm going crazy, but it's definitely a WordPress issue. I am trying to get the ID of the user from the database (the user is not logged in yet). If given the following query:

    $print_user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = '$user_login_email'");
    echo $print_user_id;

    Where $user_login_email = something like "foo@bar.com". When I run this, it spits out nothing. If the user_login is not an email address, it works fine, but if it is, no dice. I've created another PHP file and run it manually outside of the WordPress environment and it works fine. I played around with different quotes, etc, to no avail. Am I missing something? Thank you for your help in advance.

    [No bumping - especially after only 1 hour. If it's that urgent, consider hiring someone.]

  2. Chris
    Member
    Posted 1 year ago #

    You can get a custom user object without having to query the DB by yourself like this:
    $User = get_user_by( 'email', $user_login_email );
    $User->ID would then give you the ID, for example.

  3. griffgj
    Member
    Posted 1 year ago #

    Good idea, however, I still have a problem. I create a file with the following code only:

    require('wp-blog-header.php');
    $User = get_user_by( 'email', 'teats@griffshp.com' );
    echo $User->ID;

    When I put this in the root of my WordPress site, it works. When it's in the plugins directory it doesn't. Same file (except I point to the wp-blog-header.php file differently). What's going on? Any ideas?

  4. bcworkz
    Member
    Posted 1 year ago #

    Do you have any rewrites or permissions set that may affect access? Your page (with an email valid for my site) works fine in either location on my installation.

  5. Chris
    Member
    Posted 1 year ago #

    Just recognized: Have you tried WHERE user_email instead of WHERE user_login in your SQL query?

Topic Closed

This topic has been closed to new replies.

About this Topic