How can i retrieve the author id if i have its name.

I have fornt end form any user can enter the author name to filter the posts by author name so after that how can i get author id. Or there is any other way to write the query so that it return the posts which is posted by specific authors. Thanks


get_user_by will get you the user data by 'id', 'slug', 'email', or 'login'.

'slug' is the user_nicename.

$user = get_user_by('slug','nicename');

However, I am not sure what you mean by 'name'. There are other plausible 'name' fields, not all of them required. There is the display_name for example.

To search fields like user_nicename you will need to create a new WP_User_Query.

$args= array(
  'search' => 'Display Name',
$user = new WP_User_Query($args);

The default columns searched appear to be based on the type of data in the search field but you can restrict to particular fields by using the search_fields parameter.

$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);
  • Yes here [name] means [display_name]. Basically i get the author [display_name] from user and on the bases of that [dis_play] name i show the posts the posts. > $user = get_user_by('slug', 'nicename'); Can i put the [display_name] instead of [nicename] here? – Adi Mar 8 '13 at 18:51

Strange, because I found it difficult the correct answer to this, in turn anyway, it's a simple thing: Firstly you need get the author's name (So this now you've got it this way either, in this example I get the comment author's name):

$author = get_comment_author( $comment_ID );

And now the ID by its name:

$the_user = get_user_by('login', '' . $author . ''); $the_user_id = $the_user->ID;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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