8 Ways to Get User ID in WordPress
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:
- 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. 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:

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');

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
Hi!
The last method you told i.e. using get_current_user_id() doesn’t work when I use it outside admin_page function. Let me show you an example:
here outside, get_current_user_id() returns 0 . Can you explain me why? Or is there any file I forgot to include/require which declares get_current_user_id() for me. By the way I’ve tried including wp-includes/user.php where the function resides currently..
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: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..