Skip to content

Instantly share code, notes, and snippets.

@jonathanstegall
Last active June 30, 2020 19:35
Show Gist options
  • Select an option

  • Save jonathanstegall/d46ddffbf0199fca402bd07e5c3a1184 to your computer and use it in GitHub Desktop.

Select an option

Save jonathanstegall/d46ddffbf0199fca402bd07e5c3a1184 to your computer and use it in GitHub Desktop.
SELECT u.ID, u.user_email, count(distinct ifnull(c.comment_ID, 0)) as count,
(
SELECT GROUP_CONCAT( m.meta_value )
FROM wp_usermeta m
WHERE m.meta_key = 'member_level' AND m.user_id = u.ID AND m.meta_value != 'Non-member'
) as member_level
FROM wp_users u
JOIN wp_comments c ON u.ID = c.user_id
WHERE comment_approved = 1 AND comment_type IN ("comment", "") AND c.user_id <> 0
GROUP BY u.ID
ORDER BY count DESC
@jonathanstegall
Copy link
Copy Markdown
Author

jonathanstegall commented Jun 30, 2020

Use this to also check if they've already got a setting to load all the comments. This puts them to the bottom if they do have that field, no matter how many comments they've got:

SELECT u.ID, u.user_email, count(distinct ifnull(c.comment_ID, 0)) as count,
(
	SELECT m.meta_value
	FROM wp_usermeta m
	WHERE m.meta_key = 'always_load_comments' AND m.user_id = u.ID
) as comment_load_status
FROM wp_users u
JOIN wp_comments c ON u.ID = c.user_id
JOIN wp_usermeta m ON u.ID = m.user_id
WHERE comment_approved = 1 AND comment_type IN ("comment", "") AND c.user_id <> 0
GROUP BY u.ID
ORDER BY comment_load_status ASC, count DESC

Note: for this purpose, we don't care about the member level so it's not included.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment