All I'm trying to do is, get the author name of the current post and set it's name as a tag. I tried changing author_id to author_name but didn't really get anything back.
wp_set_post_tags should return 'user_nicename' (nickname) instead of 'hey'
All I'm trying to do is, get the author name of the current post and set it's name as a tag. I tried changing author_id to author_name but didn't really get anything back.
wp_set_post_tags should return 'user_nicename' (nickname) instead of 'hey'
Share Improve this question edited May 10, 2019 at 13:58 J patel asked May 9, 2019 at 19:50 J patelJ patel 154 bronze badges 1 |1 Answer
Reset to default 0The problem with your code lies in these three lines:
$queried_post = get_post($post_id);
$author_id = $queried_post->post_author;
$first = $user_info->user_nicename;
You correctly get authors ID, but then you try to get some field of user_info
variable, which is not defined anywhere in your code.
One way to fix it is to get user info (using get_userdata
) before trying to access it:
$author_id = $queried_post->post_author;
$user_info = get_userdata( $author_id );
$first = $user_info->user_nicename;
You can also use get_the_author_meta
to obtain only the field you need:
$author_id = $queried_post->post_author;
$first = get_the_author_meta( 'user_nicename', $author_id );
user_nicename
value, thenget_the_author_meta( 'user_nicename', $author_id )
would do it. And check the reference if you need help using theget_the_author_meta
function. – Sally CJ Commented May 9, 2019 at 20:19