Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

If I have a user's ID, what's the best way for me to get the title of that user's most recent post?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

You just set the 'author' parameter in a WP_Query query or get_posts (which accepts the same parameters):

$recent = get_posts(array(
    'author'=>1,
    'orderby'=>'date',
    'order'=>'desc',
    'numberposts'=>1
));
if( $recent ){
  $title = get_the_title($recent[0]->ID);
}else{
  //No published posts
}

(Note the 'orderby' and 'order' here are redundant because they are set to their default values, but you get the idea)

share|improve this answer
add comment

The best way would be a custom SQL query, but you can simply use the native functions like get_posts and that would be just fine as well;

$args = array(
'posts_per_page' => 1,
'post_author' => $user_id
);
$posts = get_posts($args);
If($posts){
echo $posts[0]->post_title;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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