count_many_users_posts() WordPress Function

This function will take a user ID and count the number of posts that user has made.

count_many_users_posts( int[] $users, string|string[] $post_type = 'post', bool $public_only = false ) #

Gets the number of posts written by a list of users.


Parameters

$users

(int[])(Required)Array of user IDs.

$post_type

(string|string[])(Optional) Single post type or array of post types to check. Defaults to 'post'.

Default value: 'post'

$public_only

(bool)(Optional) Only return counts for public posts. Defaults to false.

Default value: false


Top ↑

Return

(string[]) Amount of posts each user has written, as strings, keyed by user ID.


Top ↑

Source

File: wp-includes/user.php

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;
}


Top ↑

Changelog

Changelog
VersionDescription
3.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More