Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
[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