polylang / polylang

WordPress multilingual plugin
https://wordpress.org/plugins/polylang/
GNU General Public License v3.0
346 stars 106 forks source link

get_users by language #1511

Open Mte90 opened 1 month ago

Mte90 commented 1 month ago

Prerequisites

Steps to reproduce the issue

$users = get_users( [ 'role' => 'contributor', 'orderby' => 'post_count', 'order' => 'DESC', 'number' => 3 ] );

Generate this query:

SELECT wp_users.ID
FROM wp_users
INNER JOIN wp_usermeta
ON ( wp_users.ID = wp_usermeta.user_id )
LEFT OUTER JOIN (
SELECT post_author, COUNT(*) as post_count
FROM wp_posts
WHERE ( ( post_type = 'post'
AND ( post_status = 'publish'
OR post_status = 'private' ) ) )
GROUP BY post_author ) p
ON (wp_users.ID = p.post_author)
WHERE 1=1
AND ( ( ( wp_usermeta.meta_key = 'wp_capabilities'
AND wp_usermeta.meta_value LIKE '%\"contributor\"%' ) ) )
ORDER BY post_count DESC
LIMIT 0, 3

As you can see the WP_User_Query doesn't check for the language (I tried too with the lang parameter).

Expected behavior and actual behavior

I was expecting a way to order users based the amount of articles they published in a language.

Mte90 commented 1 month ago

The new query should be:

SELECT wp_users.ID
        FROM wp_users
        INNER JOIN wp_usermeta
        ON ( wp_users.ID = wp_usermeta.user_id )
        LEFT OUTER JOIN (
            SELECT ID, post_author, COUNT(*) as post_count
            FROM wp_posts
            WHERE ( ( post_type = 'post'
            AND ( post_status = 'publish'
            OR post_status = 'private' ) ) )
            GROUP BY post_author ) p
        ON (wp_users.ID = p.post_author)
        LEFT JOIN wp_term_relationships
        ON (p.ID = wp_term_relationships.object_id)
        WHERE 1=1
        AND ( wp_term_relationships.term_taxonomy_id IN (10017) )
        AND ( ( ( wp_usermeta.meta_key = 'wp_capabilities'
        AND wp_usermeta.meta_value LIKE '%\"contributor\"%' ) ) )
        ORDER BY post_count DESC
        LIMIT 0, 3