title: Finding the Latest Post by Author
body: <p>I'm building an "authors" page for my Wordpress blog that lists all the current site contributors along with various other information such as their number of published posts, the date of their last post, etc.</p>
  
  <p>Google and the Wordpress Codex has pointed me toward using subqueries in MySQL to pull all the data I need in one query, and it's working wonderfully for getting the number of published posts per author.</p>
  
  <p>What I can't get working is finding the post ID for the latest post, per author.</p>
  
  <p>Current query that works, without latest post:</p>
  
  <pre><code>SELECT users.ID, (SELECT count(*) FROM posts, users WHERE users.ID = posts.post_author AND posts.post_type = 'post' AND posts.post_status = 'publish') AS post_count FROM users ORDER BY post_count DESC
  </code></pre>
  
  <p>My attempt to get the latest post ID for each author ('latest_post_ID'):</p>
  
  <pre><code>SELECT users.ID, (SELECT count(*) FROM posts, users WHERE users.ID = posts.post_author AND posts.post_type = 'post' AND posts.post_status = 'publish') AS post_count, (SELECT posts.ID FROM posts, users WHERE users.ID = posts.post_author AND posts.post_type = 'post' AND posts.post_status = 'publish' ORDER BY posts.post_date DESC LIMIT 1) AS latest_post_ID FROM users ORDER BY post_count DESC
  </code></pre>
  
  <p>The problem being with the added subquery - the query will find any posts that are published that match any author, rather than what I'm intending ('users.ID = posts.post_author').</p>
  
  <p>I'd much appreciate it if someone with some SQL-fu could point out where I'm misusing and/or abusing MySQL subqueries.</p>