7 Ways to Get User ID in WordPress

Useful tips about getting users IDs in WordPress admin area or directly in the code. With code examples.

#users  /   19

In this tutorial I’m describing the 7 ways of getting the user IDs in WordPress. Each way is good for a different situation.

1. In User Profile URL #

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


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

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;

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;

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

6. How to Get the Post Author 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. Current User ID #

Let’s get the currently logged in user ID:

$current_user_id = get_current_user_id();

Only the best of WordPress

Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.

Comments 19

  • 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:

    add_action( 'admin_menu', 'register_admin_page' );
    function register_admin_page(){
    	add_menu_page( 'bla bla bla', 'bla bla bla', 'edit_posts', 'bla bla bla', 'admin_page', $icon_url, $position ); 
    function admin_page(){
    // I can access get_current_user_id() here... 

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

  • I have a login area on one of my pages, where users can login.
    i would to receive an email, if and which user uses the login pages.

    i am trying to GET USER ID BY EMAIL, but i cant get it to work.

    here is the code I use in the secure page..

       header('Location: something_sales_login.php');

    i tried to get it to work, the most logical i think would be…

          $the_user = get_user_by('email', 'something@something.com');
          $the_user_id = $username-&gt;ID;
       header('Location: something_sales_login.php');

    i cant get the email to send. I hope anybody will have a solution.

  • How code to display user ID for per user in buddypress members dictory page?
    My code, but it didnot working.

    function add_userid_to_members_loop() {
    	$bp = buddypress();
    	$id = !empty( $bp->displayed_user->id )
    		? $bp->displayed_user->id
    		: 0;
    echo $id;
    add_action('bp_directory_members_item',  'add_userid_to_members_loop');
    • Did you forget about } on line 6?

      • i insert


        but it isnot still working.

        I try use a your code and edit it as bellow

        function add_userid_to_members_loop($value, $column_name, $user_id) {
        	if ( 'user_id' == $column_name )
        		return $user_id;
        	return $value;
        add_action('bp_directory_members_item',  'add_userid_to_members_loop', 10, 3);

        But it isnot still work.

  • Currently trying to fetch a user ID in the footer.php of my theme. But it doesn’t seem to grab any data.
    Any clues as for why? :)

    <?php if( is_user_logged_in() ): ?>
            <a href="<?php get_current_user_id(); ?>">Link text</a>
            // some more HTML
          <?php else: ?>
           // alternative HTML
          <?php endif; ?>
  • 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?


    • Hi Bruno,

      I hope it helps:

      $user = get_user_by( 'id', get_current_user_id() );
      echo 'example.com/MjswO0JSOzM7OzA.html?mdasc=' . $user->user_login;
  • 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.

Leave your question or feedback

Please, enter a comment
Please, enter a name
Incorrect email