@daniloparrajr

How to get the author data outside the query loop

  • 8/30/2023
  • 1 min read

There are scenarios where we want to get the current post’s author outside the query loop. There are multiple ways to do this but the simpliest way is by using get_post_field function.

Once we get the author id, we can now use the get_the_author_meta function to get the author’s meta data e.g. display name.

// Get the current post id.
$post_id = get_current_queried_id();
// Get the current post author id.
$post_author_id = get_post_field( 'post_author', $post_id );
// Get the post author display name.
$author_display_name = get_the_author_meta( 'display_name', $post_author_id );

Ofcourse you can also use this inside the query loop.

Related Articles