10 Ways to Get User ID

In this tutorial we are going to dive into different ways of getting IDs of WordPress users.

Before we dive into actual ways of getting the user IDs, let me show you the opposite – how you can get a user object by ID. The long story short get_user_by() function is the best for that purpose.

$user = get_user_by( 'id', 51 ); // 51 is a user ID
echo $user->first_name; // let's pring the first name of this user

1. In URL when you edit the user 

This is a dead simple method, but it also have some disadvantages. First – you should be logged in, second – you can not obtain the ID of yourself.

So:

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

2. Add a custom column with user ID

Another non-coding method that allows to find out a specific user ID is to create a column in Users table. Just like this:

user ID column in WordPress admin

In order to make the column to be displayed just add the code below into your functions.php file (more on that here).

// register the column
add_filter( 'manage_users_columns', 'rudr_user_id_column' );

function rudr_user_id_column( $columns ) {
	$columns[ 'user_id' ] = 'ID';
	return $columns;
}
// populate the column with data
add_action( 'manage_users_custom_column',  'rudr_column_content', 10, 3);

function rudr_column_content( $value, $column_name, $user_id ) {
	if( 'user_id' === $column_name ) {
		return $user_id;
	}
	return $value;
}
// CSS for the column
add_action( 'admin_head-users.php',  'rudr_column_style' );

function rudr_column_style(){
	echo '<style>.column-user_id{ width: 5% }</style>';
}

3-4. Get current user ID in WordPress

get_current_user_id()

The simplest way to get an ID of a currently logged in user who is viewing the specific page (where the code is running).

$current_user_id = get_current_user_id();

wp_get_current_user()

Another way is to use wp_get_current_user() function. It will be useful when you need to get not only an ID of a current user but also some additional data like first name or email.

$current_user = wp_get_current_user();

// current user ID
$current_user_id = $current_user->ID;
// current user email
$current_user_email = $current_user->user_email;
// current user login (username)
$current_user_login = $current_user->user_login;

$current_user_name = array(
	'first' => $current_user->first_name,
	'last' => $current_user->last_name
);

There is also get_currentuserinfo() but now it is deprecated, so please don’t use it.

5. Get user ID by email

Simple enough. You can easily use get_user_by() function I already mentioned before.

$user = get_user_by( 'email', 'no-reply@rudrastyh.com' );
$user_id = $user->ID;

Doing the opposite – how to get user email by ID

get_user_by() will help you with that as well

$user = get_user_by( 'id', 54 );
echo $user->user_email;

By you can also use get_userdata().

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

6. Get user ID by username (login)

In the following example the second parameter “rudrastyh” is the username

$user = get_user_by('login', 'rudrastyh');
$user_id = $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.

7-8. 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.';
}

The thing here is that I used get_results() method instead of get_var() because there could be multiple users with the same 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.

9. 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

10. Get customer ID from WooCommerce order?

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 WooCommerce 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
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter