Proper way to work with wpdb to get user’s firstname

I want to use $wpdb to get user’s firstname. I know how to do it with php and mysql query, but I want to use $wpdb.

The problem is that I don’t know how to use it. I tried to create simple
query to display user fist_name by user_id and than echo it. This is what I ended up with:

$mywpdb1 = $wpdb->get_var( 
    $wpdb->prepare( 
        "SELECT first_name 
        FROM $wpdb->usermeta 
        WHERE user_id = 2", 
        $user->first_name
    ) 
); 
echo $mywpdb1;

2 Answers
2

You should’nt really be using wp query here, use get_user_meta();

Example:

$first_name = get_user_meta(2, 'first_name', true);

Leave a Comment