Return ID of authors who have at least one post

Instead of a JOIN, you can use a IN with a subquery, to prevent duplicate results because of the join:

SELECT ID, display_name
FROM wp_users
WHERE ID IN (SELECT post_author FROM wp_posts)

This will not look at capabilities. If you also want that you can add them like a join, but move the meta_key clause in the join, also to prevent duplicate results:

SELECT ID, display_name
FROM wp_users
    JOIN wp_usermeta ON (wp_users.ID = wp_usermeta.user_id AND wp_usermeta.meta_key = 'wp_capabilities')
WHERE ID IN (SELECT post_author FROM wp_posts)
    AND (wp_usermeta.meta_value LIKE '%"author"%'
        OR wp_usermeta.meta_value LIKE '%"editor"%' )

Up to you to convert this back to PHP! Sidenote: Is the wp_ in wp_capabilities dependent on the database prefix, or is it always wp_, even if your table names start with another prefix?

Leave a Comment