{"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

$post_id = 257;\n$author_id = get_post_field( 'post_author', $post_id );<\/code><\/pre>\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

How to get Author Name from a Post ID<\/h2>\n\n\n\n

To get the author display name from a post ID, use this code:<\/p>\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

<\/p>\n\n\n\n

What other information can you get from get_post_field<\/em>?<\/h2>\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

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

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":"\nHow To Get The WordPress Author ID From A Post ID<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Get The WordPress Author ID From A Post ID\" \/>\n<meta property=\"og:description\" content=\"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\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/\" \/>\n<meta property=\"og:site_name\" content=\"Intelliwolf\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-10T22:47:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-09-16T22:36:25+00:00\" \/>\n<meta name=\"author\" content=\"Mike Haydon\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mike Haydon\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/\"},\"author\":{\"name\":\"Mike Haydon\",\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/person\/7209e3ff14822e4d70d5f194a310f343\"},\"headline\":\"How To Get The WordPress Author ID From A Post ID\",\"datePublished\":\"2020-09-10T22:47:32+00:00\",\"dateModified\":\"2020-09-16T22:36:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/\"},\"wordCount\":533,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/www.intelliwolf.com\/#organization\"},\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/\",\"url\":\"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/\",\"name\":\"How To Get The WordPress Author ID From A Post ID\",\"isPartOf\":{\"@id\":\"https:\/\/www.intelliwolf.com\/#website\"},\"datePublished\":\"2020-09-10T22:47:32+00:00\",\"dateModified\":\"2020-09-16T22:36:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.intelliwolf.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"General\",\"item\":\"https:\/\/www.intelliwolf.com\/category\/general\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How To Get The WordPress Author ID From A Post ID\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.intelliwolf.com\/#website\",\"url\":\"https:\/\/www.intelliwolf.com\/\",\"name\":\"Intelliwolf\",\"description\":\"WordPress, Web Design and Coding Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.intelliwolf.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.intelliwolf.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.intelliwolf.com\/#organization\",\"name\":\"Intelliwolf\",\"url\":\"https:\/\/www.intelliwolf.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-content\/uploads\/intelliwolf-logo-300t.png\",\"contentUrl\":\"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-content\/uploads\/intelliwolf-logo-300t.png\",\"width\":300,\"height\":100,\"caption\":\"Intelliwolf\"},\"image\":{\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/person\/7209e3ff14822e4d70d5f194a310f343\",\"name\":\"Mike Haydon\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d5f4754fae310a04dede91d15e57c8a0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d5f4754fae310a04dede91d15e57c8a0?s=96&d=mm&r=g\",\"caption\":\"Mike Haydon\"},\"sameAs\":[\"https:\/\/intelliwolf.com\/about-mike-haydon\/\"]}]}<\/script>\n","yoast_head_json":{"title":"How To Get The WordPress Author ID From A Post ID","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/","og_locale":"en_US","og_type":"article","og_title":"How To Get The WordPress Author ID From A Post ID","og_description":"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","og_url":"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/","og_site_name":"Intelliwolf","article_published_time":"2020-09-10T22:47:32+00:00","article_modified_time":"2020-09-16T22:36:25+00:00","author":"Mike Haydon","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mike Haydon","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/#article","isPartOf":{"@id":"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/"},"author":{"name":"Mike Haydon","@id":"https:\/\/www.intelliwolf.com\/#\/schema\/person\/7209e3ff14822e4d70d5f194a310f343"},"headline":"How To Get The WordPress Author ID From A Post ID","datePublished":"2020-09-10T22:47:32+00:00","dateModified":"2020-09-16T22:36:25+00:00","mainEntityOfPage":{"@id":"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/"},"wordCount":533,"commentCount":4,"publisher":{"@id":"https:\/\/www.intelliwolf.com\/#organization"},"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/","url":"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/","name":"How To Get The WordPress Author ID From A Post ID","isPartOf":{"@id":"https:\/\/www.intelliwolf.com\/#website"},"datePublished":"2020-09-10T22:47:32+00:00","dateModified":"2020-09-16T22:36:25+00:00","breadcrumb":{"@id":"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.intelliwolf.com\/get-author-id-from-post-id\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.intelliwolf.com\/"},{"@type":"ListItem","position":2,"name":"General","item":"https:\/\/www.intelliwolf.com\/category\/general\/"},{"@type":"ListItem","position":3,"name":"How To Get The WordPress Author ID From A Post ID"}]},{"@type":"WebSite","@id":"https:\/\/www.intelliwolf.com\/#website","url":"https:\/\/www.intelliwolf.com\/","name":"Intelliwolf","description":"WordPress, Web Design and Coding Tutorials","publisher":{"@id":"https:\/\/www.intelliwolf.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.intelliwolf.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.intelliwolf.com\/#organization","name":"Intelliwolf","url":"https:\/\/www.intelliwolf.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.intelliwolf.com\/#\/schema\/logo\/image\/","url":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-content\/uploads\/intelliwolf-logo-300t.png","contentUrl":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-content\/uploads\/intelliwolf-logo-300t.png","width":300,"height":100,"caption":"Intelliwolf"},"image":{"@id":"https:\/\/www.intelliwolf.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.intelliwolf.com\/#\/schema\/person\/7209e3ff14822e4d70d5f194a310f343","name":"Mike Haydon","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.intelliwolf.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d5f4754fae310a04dede91d15e57c8a0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d5f4754fae310a04dede91d15e57c8a0?s=96&d=mm&r=g","caption":"Mike Haydon"},"sameAs":["https:\/\/intelliwolf.com\/about-mike-haydon\/"]}]}},"_links":{"self":[{"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/posts\/1646"}],"collection":[{"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/comments?post=1646"}],"version-history":[{"count":3,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/posts\/1646\/revisions"}],"predecessor-version":[{"id":1905,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/posts\/1646\/revisions\/1905"}],"wp:attachment":[{"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/media?parent=1646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/categories?post=1646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/tags?post=1646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}