Show the title of the latest post by author

I have the following function which I use to show the author name,author avatar and the author biography in a div. I need to show the title of the latest post by the author along with this. Can anyone help?

function ajaxified_function() 
{ 
    $response = new WP_Ajax_Response();
    $id        = $_POST['author_id'];
    $auth_name = get_the_author_meta('display_name', $id);
    $avatar    = get_avatar($id);
    $desc      = get_the_author_meta('description',$id);
    $auth_desig  = get_the_author_meta('designation', $id);
    $output = "<div id='bloggers_title'>$auth_name</div>n
           <div id='bloggers_desig'>$auth_desig</div>n
           <div id='bloggers_avatar'>$avatar</div>n
           <div id='bloggers_desc'>$desc</div>n";
    $response->add(array(
    'what' => 'has',
    'data' => $output
)); 
$response->send();
}

Related posts

Leave a Reply

1 comment

  1. You can get the latest post of an author adding the following code to your function:

    $latest_post = get_posts( array(
            'author'      => $id,
            'orderby'     => 'date',
            'numberposts' => 1
    ));
    
    // Since get_posts() returns an array, but we know we only
    // need one element, let's just get the element we need.
    $latest_post = $latest_post[0];
    

    Then modify your $output, adding the data you need (specifically guid for the permalink, and post_title for the title), for example:

    $output .= "<div id='bloggers_latest_post'>
                    <a href='$latest_post->guid'>$latest_post->post_title</a>
                </div>"