How To Get The WordPress Author ID From A Post ID

I was building a gift registry system where users could create and customize their own registry pages.

I needed a way to connect the author ID to the post ID and display a different template based on who was viewing the gift registry page.

But I needed to do it outside the loop.

How to get the WordPress author ID from a post ID: The code to get the author ID from a post ID outside the loop is get_post_field( 'post_author', $post_id ); where $post_id is the ID of the post or page you are looking for.

Here's the code in a more clear format.

$post_id = 257;
$author_id = get_post_field( 'post_author', $post_id );

You'll get back a string with just the ID of the author.

In this example, I'm assuming you're just after the author for the page with ID 257. In reality, you'll probably get that programmatically, rather than hard coding like this.

This is the same format for all pages and posts. WordPress treats pages as just a type of post.

If you're working with a WordPress post object, you don't need to extract the ID first. You can pass the object into the function instead of $post_id.

What you do with the Author ID is up to you, but the next step is often to convert the Author ID into the chosen display name of that Author.

How to get Author Name from a Post ID

To get the author display name from a post ID, use this code:

$post_id = 257;
$author_id = get_post_field( 'post_author', $post_id );
$author_name = get_the_author_meta( 'display_name', $author_id );

What other information can you get from get_post_field?

By changing the first parameter of get_post_field, ie the 'post_author' in the earlier examples, you can extract the information in the table below.

I ran this on a sample page from a default install. Your information will be different.

Note that the parameters in the field column are case sensitive. So you have to use "ID" rather than "id".

FieldResult
ID2
post_author1
post_date2019-12-04 03:52:20
post_date_gmt 2019-12-04 03:52:20
post_contentThis is an example page. It's different from ...
post_titleSample Page
post_excerpt
post_statuspublish
post_namesample-page
post_parent0
post_typepage
post_modified2020-09-06 22:51:19
post_modified_gmt2020-09-06 22:51:19
comment_count0
comment_statusclosed
ping_statusclosed
post_password1234
to_ping
pinged
post_content_filtered
guidhttp://localhost/demo/?page_id=2
menu_order0
post_mime_type
filterraw

As you can see, a number of the columns are empty.

The post_content will pull the full content, complete with line breaks. I just reduced it to make it easy to display in the table.

Notice that the dates are formatted Year/Month/Day. That makes it easy to figure out, rather than the confusion over whether it's using the regular format (day/month), or the US format (month/day).

The post_name field is also called the slug or the permalink. The different names it uses in different places can cause some confusion if you've not come across it before.

The post_password field displays in plain text, rather than being encrypted. This is also how it's stored in the database.

Leave a Comment