Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get author ID of a post in wordpress

Tags:

Im creating a loop where it pulls info about a users activity. the code is on the author page and what i want to to is get the current author ID, so based on what author page the user is on it will fetch the ID of that author.

See below, i would want AUTHOR-ID to be the ID of the current author.

<script type="text/javascript">                             var pieData = [                             <?php                              $user_id = AUTHOR-ID;                              /* Get all categories */                             $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');                               $categories = get_terms( 'category', 'orderby=count&hide_empty=0' );                              /* Loop for each category to count the posts of the user */                             foreach($categories as $category)                             {                             $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];                                $cat_name = $category->name;                                $cat_id = $category->term_id;                                $post_count = count(get_posts("cat=$cat_id&post_author=$user_id"));                                 echo "                                              {                                                 value: ".$post_count.",                                                 color:'".$color."',                                                 label: ".$user_id."                                             },";                              }                             ?>                             ]                             var myPie = new Chart(document.getElementById("piec").getContext("2d")).Pie(pieData);                         </script> 
like image 360
Zackskeeter Avatar asked Oct 04 '13 08:10

Zackskeeter


People also ask

How do I get post author from post ID?

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 );

How do I find the author image in WordPress?

The easiest way to add an author photo in WordPress is by using the PublishPress Authors plugin. This plugin lets authors edit their own profile and author photo by adding a new 'Author Profile' area to their WordPress dashboard.

What is an author in WordPress?

The author role is for users that you want to write and publish content for your WordPress blog. Authors can write, edit, and publish their own posts, but can't modify posts written by other users. They can upload files and add their own images to posts.


1 Answers

Try with this :

<?php $author_id=$post->post_author; ?> 

it will give you current author id.

or this one will helps you more:

global $current_user; get_currentuserinfo();                        $args = array(     'author'        =>  $current_user->ID, // I could also use $user_ID, right?             );  // get his posts 'ASC' $current_user_posts = get_posts( $args ); 

Thanks.

like image 70
Krunal Shah Avatar answered Sep 30 '22 09:09

Krunal Shah