8 Ways to Get User ID in WordPress

In this tutorial I will show you code examples on how to find current user ID, how to get user ID by username, email and meta data. All the examples work for WooCommerce too.

/19 comments

1. Find User ID in WordPress Admin Area

This is a very simple method, but it also have some disadvantages. First – you should be logged in, second – you can not obtain the id of your own user.

So:

  1. Log into your WordPress admin
  2. Go to Users > All users
    All users link in the menu
  3. Choose the user and go to his profile
  4. Look at the URL of the page:
    You can obtain the user ID from his profile URL.

2. Get Current User ID (and username, email etc)

The best way to get a currently logged in user ID is using get_current_user_id() function.

$current_user_id = get_current_user_id();

Absolutely the same way is with wp_get_current_user():

$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;

The usage of get_current_user_id() seems simpler for me but you can use any way you want, because in the code they are the same. But wp_get_current_user() allows you at the same time to get current user email $current_user->user_email, first name $current_user->first_name, last name $current_user->last_name, username $current_user->user_login and display name $current_user->display_name.

But did you hear about get_currentuserinfo()? Should you use it? The answer is – no, this function is deprecated. Use wp_get_current_user() instead.

3. How to Get User ID by Email?

Simple enough. Just use get_user_by() function.

$the_user = get_user_by('email', 'misha@rudrastyh.com');
$the_user_id = $the_user->ID;

Bonus: Get User Email by ID

First of all you can get user email with the same get_user_by() function.

$the_user = get_user_by( 'id', 54 ); // 54 is a user ID
echo $the_user->user_email;

The next method is just get_userdata() function, which is equal to get_user_by( 'id', ... )

$the_user = get_userdata( 54 );
echo $the_user->user_email;

4. Get User ID by Username (login name)

In the following example the second parameter "rudrastyh" is the username.

$the_user = get_user_by('login', 'rudrastyh');
$the_user_id = $the_user->ID;

Is there a way to get username from user ID? Yes, just use get_userdata() or get_user_by( 'id', ... ). Just like the example with emails.

5. Get User ID by First Name or by Last Name

Print the ids of all users with the first name is "Misha":

global $wpdb;
$users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'first_name' AND meta_value = 'Misha'" );
if( $users ) {
	foreach ( $users as $user ) {
		echo '<p>' . $user->user_id . '</p>';
	}
} else {
	echo 'There are no users with the specified first name.';
}

Print the ids of all users with the last name is "Rudrastyh":

global $wpdb;
$users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'first_name' AND meta_value = 'Rudrastyh'" );
if( $users ) {
	foreach ( $users as $user ) {
		echo '<p>' . $user->user_id . '</p>';
	}
} else {
	echo 'There are no users with the specified last name.';
}

The code above will work if you would like to find user ID by any user meta value. Just replace meta_key and meta_value with the ones you need.

And of course you can get user first and last names and any meta with the help of get_user_meta( $id, $meta_key, true) function.

6. How to Get Author ID by Post ID?

In this case you can get the user ID from WP_Post object. It’s simple.

$my_post = get_post( $id ); // $id - Post ID
echo $my_post->post_author; // print post author ID

7. How to Get a Customer ID from an Order in WooCommerce?

There are two different ways to do it, the first one is just to get customer ID from order meta this way:

$customer_id = get_post_meta( 541, '_customer_user', true); // 541 is your order ID

The second one is with the help of WC_Order object. By the way this method will work only for WC 3.0+.

$order = wc_get_order( 541 ); // 541 is your order ID
$customer_id = $order->get_customer_id(); // or $order->get_user_id() – the same

8. Add the User ID column to the WordPress Users Table

Very useful way of getting user ids through WordPress admin. Here is the result:

User ID column in the users table

How to create the same column on you own WordPress website? All you need to do is to copy the following code to you current theme functions.php file:

/*
 * Adding the column
 */
function rd_user_id_column( $columns ) {
	$columns['user_id'] = 'ID';
	return $columns;
}
add_filter('manage_users_columns', 'rd_user_id_column');
 
/*
 * Column content
 */
function rd_user_id_column_content($value, $column_name, $user_id) {
	if ( 'user_id' == $column_name )
		return $user_id;
	return $value;
}
add_action('manage_users_custom_column',  'rd_user_id_column_content', 10, 3);
 
/*
 * Column style (you can skip this if you want)
 */
function rd_user_id_column_style(){
	echo '<style>.column-user_id{width: 5%}</style>';
}
add_action('admin_head-users.php',  'rd_user_id_column_style');

Related

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WordPress?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Comments — 19

Leave a comment

php js HTML CSS Code