{"id":1646,"date":"2020-09-11T06:47:32","date_gmt":"2020-09-10T22:47:32","guid":{"rendered":"https:\/\/www.intelliwolf.com\/?p=1646"},"modified":"2020-09-17T06:36:25","modified_gmt":"2020-09-16T22:36:25","slug":"get-author-id-from-post-id","status":"publish","type":"post","link":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/get-author-id-from-post-id\/","title":{"rendered":"How To Get The WordPress Author ID From A Post ID"},"content":{"rendered":"\n
I was building a gift registry system where users could create and customize their own registry pages.<\/p>\n\n\n\n
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.<\/p>\n\n\n\n
But I needed to do it outside the loop.<\/p>\n\n\n\n
How to get the WordPress author ID from a post ID<\/strong>: The code to get the author ID from a post ID outside the loop is get_post_field( 'post_author', $post_id );<\/em> where $post_id is the ID of the post or page you are looking for.<\/p>\n\n\n\n Here's the code in a more clear format.<\/p>\n\n\n\n You'll get back a string with just the ID of the author.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n This is the same format for all pages and posts. WordPress treats pages as just a type of post.<\/p>\n\n\n\n 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<\/em>.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n To get the author display name from a post ID, use this code:<\/p>\n\n\n\n <\/p>\n\n\n\n By changing the first parameter of get_post_field<\/em>, ie the 'post_author'<\/em> in the earlier examples, you can extract the information in the table below.<\/p>\n\n\n\n I ran this on a sample page from a default install. Your information will be different.<\/p>\n\n\n\n Note that the parameters in the field<\/em> column are case sensitive. So you have to use \"ID\" rather than \"id\".<\/p>\n\n\n\n As you can see, a number of the columns are empty.<\/p>\n\n\n\n The post_content<\/em> will pull the full content, complete with line breaks. I just reduced it to make it easy to display in the table.<\/p>\n\n\n\n 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).<\/p>\n\n\n\n The post_name<\/em> 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.<\/p>\n\n\n\n The post_password<\/em> field displays in plain text, rather than being encrypted. This is also how it's stored in the database.<\/p>\n","protected":false},"excerpt":{"rendered":" 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<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"yoast_head":"\n$post_id = 257;\n$author_id = get_post_field( 'post_author', $post_id );<\/code><\/pre>\n\n\n\n
How to get Author Name from a Post ID<\/h2>\n\n\n\n
$post_id = 257;\r\n$author_id = get_post_field( 'post_author', $post_id );\n$author_name = get_the_author_meta( 'display_name', $author_id );<\/code><\/pre>\n\n\n\n
What other information can you get from get_post_field<\/em>?<\/h2>\n\n\n\n
Field<\/th> Result<\/th><\/tr><\/thead> ID<\/td> 2<\/td><\/tr> post_author<\/td> 1<\/td><\/tr> post_date<\/td> 2019-12-04 03:52:20<\/td><\/tr> post_date_gmt\u00a0<\/td> 2019-12-04 03:52:20<\/td><\/tr> post_content<\/td> This is an example page. It's different from ...<\/td><\/tr> post_title<\/td> Sample Page<\/td><\/tr> post_excerpt<\/td> <\/td><\/tr> post_status<\/td> publish<\/td><\/tr> post_name<\/td> sample-page<\/td><\/tr> post_parent<\/td> 0<\/td><\/tr> post_type<\/td> page<\/td><\/tr> post_modified<\/td> 2020-09-06 22:51:19<\/td><\/tr> post_modified_gmt<\/td> 2020-09-06 22:51:19<\/td><\/tr> comment_count<\/td> 0<\/td><\/tr> comment_status<\/td> closed<\/td><\/tr> ping_status<\/td> closed<\/td><\/tr> post_password<\/td> 1234<\/td><\/tr> to_ping<\/td> <\/td><\/tr> pinged<\/td> <\/td><\/tr> post_content_filtered<\/td> <\/td><\/tr> guid<\/td> http:\/\/localhost\/demo\/?page_id=2<\/td><\/tr> menu_order<\/td> 0<\/td><\/tr> post_mime_type<\/td> <\/td><\/tr> filter<\/td> raw<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n