A
A
Alex2015-11-13 17:00:03
PHP
Alex, 2015-11-13 17:00:03

How to get an array of users with the number of articles each has?

Now I do this:

// Массив всех пользователей
    $users = get_users( array(
      'meta_key'	=> '_cms_user_rating',
      'orderby'		=> 'meta_value_num',
      'order'		=> 'DESC',
    ) );
    // Количество записей для каждого пользователя
    $counts = count_many_users_posts(array_map(function($u){return $u->ID;}, $users));

Is there any way to get all users sorted by a custom field and immediately get the number of records each user has?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valera Karmanov, 2015-11-13
@motokraft

Getting a list of users is not a problem. Where is the article itself located and how is it determined who the author of the article is?
So you need a SQL query, or ready-made code???

N
nozzy, 2015-11-13
@nozzy

Didn't check:

select 
u.id,
u.user_login,
p.count_posts  
from wp_users u  
inner join 
(
    select  
    post_author, 
    count(*) as count_posts  
    from wp_posts
    where post_type = 'post'
    group by post_author  
) p on p.post_author = u.id
order by u.id,
p.count_posts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question