count_many_users_posts

The timeline below displays how wordpress function count_many_users_posts has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Gets the number of posts written by a list of users.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int[]           $users       Array of user IDs.
 * @param string|string[] $post_type   Optional. Single post type or array of post types to check. Defaults to 'post'.
 * @param bool            $public_only Optional. Only return counts for public posts.  Defaults to false.
 * @return string[] Amount of posts each user has written, as strings, keyed by user ID.
 */
function count_many_users_posts($users, $post_type = 'post', $public_only = false)
{
    global $wpdb;
    $count = array();
    if (empty($users) || !is_array($users)) {
        return $count;
    }
    $userlist = implode(',', array_map('absint', $users));
    $where = get_posts_by_author_sql($post_type, true, null, $public_only);
    $result = $wpdb->get_results("SELECT post_author, COUNT(*) FROM {$wpdb->posts} {$where} AND post_author IN ({$userlist}) GROUP BY post_author", ARRAY_N);
    foreach ($result as $row) {
        $count[$row[0]] = $row[1];
    }
    foreach ($users as $id) {
        if (!isset($count[$id])) {
            $count[$id] = 0;
        }
    }
    return $count;
}

WordPress Version: 5.4

/**
 * Number of posts written by a list of users.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int[]           $users       Array of user IDs.
 * @param string|string[] $post_type   Optional. Single post type or array of post types to check. Defaults to 'post'.
 * @param bool            $public_only Optional. Only return counts for public posts.  Defaults to false.
 * @return string[] Amount of posts each user has written, as strings, keyed by user ID.
 */
function count_many_users_posts($users, $post_type = 'post', $public_only = false)
{
    global $wpdb;
    $count = array();
    if (empty($users) || !is_array($users)) {
        return $count;
    }
    $userlist = implode(',', array_map('absint', $users));
    $where = get_posts_by_author_sql($post_type, true, null, $public_only);
    $result = $wpdb->get_results("SELECT post_author, COUNT(*) FROM {$wpdb->posts} {$where} AND post_author IN ({$userlist}) GROUP BY post_author", ARRAY_N);
    foreach ($result as $row) {
        $count[$row[0]] = $row[1];
    }
    foreach ($users as $id) {
        if (!isset($count[$id])) {
            $count[$id] = 0;
        }
    }
    return $count;
}

WordPress Version: 4.4

/**
 * Number of posts written by a list of users.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param array        $users       Array of user IDs.
 * @param string|array $post_type   Optional. Single post type or array of post types to check. Defaults to 'post'.
 * @param bool         $public_only Optional. Only return counts for public posts.  Defaults to false.
 * @return array Amount of posts each user has written.
 */
function count_many_users_posts($users, $post_type = 'post', $public_only = false)
{
    global $wpdb;
    $count = array();
    if (empty($users) || !is_array($users)) {
        return $count;
    }
    $userlist = implode(',', array_map('absint', $users));
    $where = get_posts_by_author_sql($post_type, true, null, $public_only);
    $result = $wpdb->get_results("SELECT post_author, COUNT(*) FROM {$wpdb->posts} {$where} AND post_author IN ({$userlist}) GROUP BY post_author", ARRAY_N);
    foreach ($result as $row) {
        $count[$row[0]] = $row[1];
    }
    foreach ($users as $id) {
        if (!isset($count[$id])) {
            $count[$id] = 0;
        }
    }
    return $count;
}

WordPress Version: 4.3

/**
 * Number of posts written by a list of users.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb
 *
 * @param array        $users       Array of user IDs.
 * @param string|array $post_type   Optional. Single post type or array of post types to check. Defaults to 'post'.
 * @param bool         $public_only Optional. Only return counts for public posts.  Defaults to false.
 * @return array Amount of posts each user has written.
 */
function count_many_users_posts($users, $post_type = 'post', $public_only = false)
{
    global $wpdb;
    $count = array();
    if (empty($users) || !is_array($users)) {
        return $count;
    }
    $userlist = implode(',', array_map('absint', $users));
    $where = get_posts_by_author_sql($post_type, true, null, $public_only);
    $result = $wpdb->get_results("SELECT post_author, COUNT(*) FROM {$wpdb->posts} {$where} AND post_author IN ({$userlist}) GROUP BY post_author", ARRAY_N);
    foreach ($result as $row) {
        $count[$row[0]] = $row[1];
    }
    foreach ($users as $id) {
        if (!isset($count[$id])) {
            $count[$id] = 0;
        }
    }
    return $count;
}

WordPress Version: 3.7

/**
 * Number of posts written by a list of users.
 *
 * @since 3.0.0
 *
 * @param array $users Array of user IDs.
 * @param string $post_type Optional. Post type to check. Defaults to post.
 * @param bool $public_only Optional. Only return counts for public posts.  Defaults to false.
 * @return array Amount of posts each user has written.
 */
function count_many_users_posts($users, $post_type = 'post', $public_only = false)
{
    global $wpdb;
    $count = array();
    if (empty($users) || !is_array($users)) {
        return $count;
    }
    $userlist = implode(',', array_map('absint', $users));
    $where = get_posts_by_author_sql($post_type, true, null, $public_only);
    $result = $wpdb->get_results("SELECT post_author, COUNT(*) FROM {$wpdb->posts} {$where} AND post_author IN ({$userlist}) GROUP BY post_author", ARRAY_N);
    foreach ($result as $row) {
        $count[$row[0]] = $row[1];
    }
    foreach ($users as $id) {
        if (!isset($count[$id])) {
            $count[$id] = 0;
        }
    }
    return $count;
}