9

I want to get all posts by certain author id (current user). Later, I want to pick the first post made by this user (ASC). I guess I do not use the right arguments in get_posts, am I? $current_user_posts always contains an Array with all blog posts in multiple different WP_Post Objects.

global $current_user;
get_currentuserinfo();                      

$args = array(
    'author'        =>  $current_user->ID, // I could also use $user_ID, right?
    'orderby'       =>  'post_date',
    'order'         =>  'ASC' 
    );

// get his posts 'ASC'
$current_user_posts = get_posts( $args );
  • get_currentuserinfo() is deprecated since version 4.5.0. Replace with: $current_user = wp_get_current_user(); – Christian Lescuyer May 15 '17 at 8:43
16

I'm a bit confused. If you want to get onlya element from the posts array you can get it like this:

  • reset($current_user_posts) - first post
  • end($current_user_posts) - lat post

But if you want to get just one post with the get_posts() you can use the posts_per_page argument to limit the results.

$args = array(
    'author'        =>  $current_user->ID,
    'orderby'       =>  'post_date',
    'order'         =>  'ASC',
    'posts_per_page' => 1
    );

More info about parameters you can get on WP Query Class Reference page (get_posts() takes same parameters as WP Query).

  • your $args work fine but I don't get your first answer. How to use $current_user_posts. Could you show me? – kindo Aug 12 '13 at 7:59
  • If you want to print the title of the first post you should use: echo $current_user_posts[0]['title']. The 'title' is the key for what you need from array. The full list of keys you cang get with print_r(array_keys($current_user_posts)). "How to use" it depends on what you want to do with it. – Marin Bînzari Aug 12 '13 at 8:17
  • get the author's first post's id – kindo Aug 12 '13 at 8:28
  • You can get the id with: $current_user_posts[0]['ID'] – Marin Bînzari Aug 12 '13 at 8:29
  • @kindo, did it helped? Is this the answer you needed? – Marin Bînzari Aug 12 '13 at 9:05
4
global $current_user;                     

$args = array(
  'author'        =>  $current_user->ID, 
  'orderby'       =>  'post_date',
  'order'         =>  'ASC',
  'posts_per_page' => -1 // no limit
);


$current_user_posts = get_posts( $args );
$total = count($current_user_posts);

and just loop the current user posts

  • Can you also explain what the above code does in addtion to posting the code, it will be helpful, thanks – bravokeyl Sep 9 '16 at 5:11
1

its work by (wp4.9.7)

 $user_id = get_current_user_id();
 $args=array(
 'post_type' => 'POSTTYPE',
 'post_status' => 'publish',
 'posts_per_page' => 1,
 'author' => $user_id
  );

$current_user_posts = get_posts( $args );
$total = count($current_user_posts);
wp_die( '<pre>' .  $total . '</pre>' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.