WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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
    
This doesn't seem to be working for me – EHerman Apr 8 '14 at 14:49
1  
@EHerman: without more information I couldn't tell you why. – s_ha_dum Apr 8 '14 at 14:58
    
dump of $user shows that display_name is not in sql query. Use another answer to enable it wordpress.stackexchange.com/questions/201779/… – shukshin.ivan Sep 28 '15 at 14:07

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 '13 at 13:42
    
Can you provide the error so I can see what the error is? – Mike Madern Mar 12 '13 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 '13 at 20:21
    
@s_ha_dum thanks for fixing it for me :) – Mike Madern Mar 13 '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

the wordpress user query by default doesn't allow the display_name in search column even it's added in , here is a solution http://manchumahara.com/2014/04/03/search-user-by-display-name-in-wordpress-sitewide/

Example:

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

The above query will not find for display_name

you need to use this filter

add_filter('user_search_columns', 'user_search_columns_bd' , 10, 3);

function user_search_columns_bd($search_columns, $search, $this){

    if(!in_array('display_name', $search_columns)){
        $search_columns[] = 'display_name';
    }
    return $search_columns;
}
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.