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:
- Log into your WordPress admin
- Go to Users > All users
- Choose the user and go to his profile
- Look at the URL of the page:

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:

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
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
thanks
How code to display user ID for per user in buddypress members dictory page?
My code, but it didnot working.
Did you forget about
}
on line 6?In your first code try to replace
echo $id;
withecho 'test';
. Is it work then?Hello :)
you forgot about
echo()
, so it would be:echo get_current_user_id();
I’m a moron for forgetting that – THANKS!
Hi,
Is there a way to append the registered username or ID to the destination URL after someone clicks on an affiliate link?
For example:
Let’s say a person registers on my wordpress site with the following username: usertest
The original affiliate link is: example.com/MjswO0JSOzM7OzA.html?mdasc=
When users click the final URL will be: example.com/MjswO0JSOzM7OzA.html?mdasc=usertest
I want all registered users to be added to the affiliate destination URL dynamically after clicking on any affiliate link. Is it possible?
Regards,
Bruno.
Hi Bruno,
I hope it helps:
Hi Misha,
Thank you very much for the fast reply. However, I don’t know where to use this code. Where do I add it?
Regards,
Bruno.
Hi Bruno,
I think it should be placed where your affiliate links are.
THANK YOU!!!!!!
I used the code to add a column with the ID# in the user list. I got the column but the ID# did not populate. Ideas?
Hello,
if you use the code from this post properly, everything should be OK.
I didn’t do anything to the code other than paste it into the file you suggested. Like I said, The column was added to the customer list … but no id numbers were listed.
I’ve just tested the code on my own website and it works perfectly.
Can you contact me by email? – so I will try to help you.
Hi
Thanks so much for this complete post!
This solved any of my big problems!
GOOD LUCK!
Regard
Misha, your code was very helpful to get the information I needed in the All Users page. Thank you very much.
Simply wanna remark on few general things, The website pattern is perfect, the articles is very good. “If a man does his best, what else is there” by George Smith Patton, Jr..