In WordPress I need to fetch name of author who created post using author_id
.
How can I find author_name
?
Asked
Active
Viewed 8.8k times
19

Raunak Gupta
- 10,412
- 3
- 58
- 97

Rakhi Prajapati
- 880
- 1
- 12
- 24
-
Check the [docs](https://developer.wordpress.org/reference/functions/the_author/) ... – Ramiz Wachtler Mar 18 '17 at 08:12
6 Answers
38
You can use
get_the_author_meta()
, to get author data.
echo get_the_author_meta('display_name', $author_id);
Hope this helps!

Raunak Gupta
- 10,412
- 3
- 58
- 97
-
-
1@RakhiPrajapati: oo isit? I did't knew neither the official doc say anything about it. – Raunak Gupta Mar 18 '17 at 09:48
-
2`get_the_author_meta()` is not depreciated. You might be thinking of `get_the_author()` – RockyK Jul 06 '21 at 21:36
-
1
-
@Akshaykn `$author_id` refers to the user ID which belongs to `wp_users.id` field. – Raunak Gupta Dec 17 '21 at 08:13
-
If you are looking for the author name of the current post (say on a single.php page) you can use `get_the_author_meta('display_name', get_the_author_meta( 'ID' ) )` to dynamically get the value of $author_id. – MillerMedia Jun 17 '22 at 19:15
10
This should work like charm
<?php echo get_the_author(); ?>
For more detailed information. https://codex.wordpress.org/Function_Reference/get_the_author

Amit
- 187
- 2
- 15
6
Use below code in single.php or the relevant page you want author name
<?php get_the_author_meta( 'display_name', $author_id ); ?>

Nikhil Chopde
- 61
- 3
4
When used in the WordPress Custom REST API endpoint you can do it like this:
function customrestapiplugin_getpost( $slug ) {
$args = [
'name' => $slug['slug'],
'post_type' => 'post'
];
$post = get_posts($args);
$data[$i]['id'] = $post[0]->ID;
$data['title'] = $post[0]->post_title;
$data['content'] = $post[0]->post_content;
$data['excerpt'] = $post[0]->post_excerpt;
$data['slug'] = $post[0]->post_name;
$data['date'] = $post[0]->post_date;
$data['link'] = get_permalink($post[0]->ID);
$data['author'] = get_the_author_meta('display_name', $post[0]->post_author);
$data['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post[0]->ID, 'thumbnail');
$data['featured_image']['medium'] = get_the_post_thumbnail_url($post[0]->ID, 'medium');
$data['featured_image']['large'] = get_the_post_thumbnail_url($post[0]->ID, 'large');
return $data;
}

MuturiAlex
- 338
- 1
- 8
3
Add This code in single-post.php
<?php echo get_the_author(); ?>
I hope this will work !!

Jawad Abdani
- 337
- 1
- 9
0
For those who are looking for a seamless solution on how to get an author of a post in WordPress, this is another lightweight method.
global $wpdb;
$post_id = 12; // your post id
$post_author_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_author FROM {$wpdb->posts} WHERE ID = %d ", $post_id ) );
$author = new WP_User( $post_author_id );
$display_name = $author->display_name;
$avartar = get_avatar( $post_author_id, 30 ); // get_avatar( userid, size )
$author_url = get_author_posts_url( $post_author_id );

Dharman
- 30,962
- 25
- 85
- 135

Karue Benson Karue
- 957
- 12
- 26