Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have user display_name and with this I want to get the id of that user.

So, how can I get the user id?

share|improve this question

3 Answers

Use WP_User_Query.

$args= array(
  'search' => 'Display Name', // or login or nicename in this example
  'search_fields' => array('user_login','user_nicename','display_name')
);
$user = new WP_User_Query($args);

Just answered one very similar to this: How we can get the author ID by its Name

share|improve this answer

You can use the following function:

function get_user_id_by_display_name( $display_name ) {
    global $wpdb;

    if ( ! $user = $wpdb->get_row( $wpdb->prepare(
        "SELECT `ID` FROM $wpdb->users WHERE `display_name` = %s", $display_name
    ) ) )
        return false;

    return $user->ID;
}

This is the same code get_user_by() uses, but since that function only allows ID, slug, email or login we have to create a new function.

share|improve this answer
The code you provide its give error. – Adi Mar 12 at 13:42
Can you provide the error so I can see what the error is? – Mike Madern Mar 12 at 14:27
unexpected T_OBJECT_OPERATOR... forgot the $ before wpdb the first time it appears in the if (fixed) – s_ha_dum Mar 12 at 20:21
@s_ha_dum thanks for fixing it for me :) – Mike Madern Mar 13 at 8:19

Completely untested, but I can't see anything in the code why it wouldn't work to use get_users() with a meta query:

$users = get_users( array(
    'meta_key' => 'display_name',
    'meta_value' => 'John Doe'
) );

$user = ( ( isset( $users[0] ) ? $users[0] : false );

$user_id = ( $user ? $user->ID : false );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.