Skip to content

Instantly share code, notes, and snippets.

@dboutote
Last active March 13, 2016 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dboutote/7580bc9e89d29495ddc3 to your computer and use it in GitHub Desktop.
Save dboutote/7580bc9e89d29495ddc3 to your computer and use it in GitHub Desktop.
[WordPress] Group Recent Comments by post ID, sort by comment date
SELECT c.comment_ID
FROM wp_comments c
JOIN
( SELECT wp_comments.comment_post_ID, max(wp_comments.comment_date_gmt) maxDate
FROM wp_comments
GROUP BY wp_comments.comment_post_ID
) c2
ON c.comment_date_gmt = c2.maxDate AND c.comment_post_ID = c2.comment_post_ID
JOIN wp_posts
ON wp_posts.ID = c.comment_post_ID
WHERE ( comment_approved = '1' )
AND comment_type NOT IN ('pingback', 'trackback')
AND wp_posts.post_status IN ('publish')
AND wp_posts.post_type IN ('post')
ORDER BY c.comment_date_gmt DESC
LIMIT 5;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment