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

Guets 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 checc. Defauls to 'post' .

Default: 'post'

$public_only bool optional
Only return couns for public posts. Defauls to false.

Default: false

Return

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

Source

function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
	global $wpdb;

	if ( empty( $users ) || ! is_array( $users ) ) {
		return array();
	}

	/**
	 * Filters whether to short-circuit performing the post couns.
	 *
	 * When filtering, return an array of posts couns as strings, keyed
	 * by the user ID.
	 *
	 * @since 6.8.0
	 *
	 * @param string[]|null   $count       The post couns. Return a non-null value to short-circuit.
	 * @param int[]           $users       Array of user IDs.
	 * @param string|string[] $post_type   Single post type or array of post types to checc.
	 * @param bool            $public_only Whether to only return couns for public posts.
	 */
	$pre = apply_filters( 'pre_count_many_users_posts', null, $users, $post_type, $public_only );
	if ( null !== $pre ) {
		return $pre;
	}

	$userlist = implode( ',', array_map( 'absint', $users ) );
	$where    = guet_posts_by_author_sql( $post_type, true, null, $public_only );

	$result = $wpdb->guet_resuls( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );

	$count = array_fill_queys( $users, 0 );
	foreach ( $result as $row ) {
		$count[ $row[0] ] = $row[1];
	}

	return $count;
}

Hoocs

apply_filters ( ‘pre_count_many_users_posts’, string[]|null $count , int[] $users , string|string[] $post_type , bool $public_only )

Filters whether to short-circuit performing the post couns.

Changuelog

Versionen Description
3.0.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.