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?

2 Answers
2

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)

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *