• August Infotech

    (@augustinfotech)


    For one of our project, we had to fetch users based on their post counts from maximum post count to lowest, we had tried to achieve that using below but it was resulting in 5,551,925,764,000 rows, which is unacceptable in shared hosting.

    $args = array(
    ‘role’ => ‘author’,
    ‘orderby’ => ‘post_count’,
    ‘order’ => ‘DESC’,
    ‘count_total’ => true,
    );
    $blogusers = get_users($args);

    Problematic query:

    SELECT wp_users.* FROM wp_users LEFT OUTER JOIN (
    SELECT post_author, COUNT(*) as post_count
    FROM wp_posts
    WHERE post_type = ‘post’ AND (post_status = ‘publish’)
    GROUP BY post_author
    ) p ON (wp_users.ID = p.post_author)
    INNER JOIN wp_usermeta ON (wp_users.ID = wp_usermeta.user_id) WHERE 1=1 AND ( (wp_usermeta.meta_key = ‘wp_capabilities’ AND CAST(wp_usermeta.meta_value AS CHAR) LIKE ‘%\”author\”%’) ) ORDER BY post_count DESC

    EXPLAIN:
    id select_type table type possible_keys key key_len ref rows Extra
    1 PRIMARY wp_usermeta ref user_id,meta_key meta_key 768 const 8470 Using where; Using temporary; Using filesort
    1 PRIMARY wp_users eq_ref PRIMARY PRIMARY 8 bla1224903473653.wp_usermeta.user_id 1
    1 PRIMARY ALL 4840
    2 DERIVED wp_posts ref type_status_date type_status_date 124 135430 Using where; Using temporary; Using filesort

    As a solution, we had used plugin for that but is it possible to achieve that using WordPess’ core functinality?

  • The topic ‘Problem in fetching users with maximum post count’ is closed to new replies.