count_many_users_posts()返回多个用户的文章总数

目录

描述

Returns the post counts for a list of user IDs. This is an O(n) operation, so it is preferred over count_user_posts() any time more than a single count is needed.

count_many_users_posts() 描述

用法

 <?php count_many_users_posts$users$post_type$public_only ); ?> 

count_many_users_posts() 用法

参数

$users

(array) (必填) List of ID values of the users whose post counts should be retrieved.

默认值: None

$post_type

(string) (可选) Name of the post type

默认值: null

$public_only

(string) (可选) Bypasses checking capabilities of current user (False) or returns posts only if they can be viewed by current user (True).

默认值: False

count_many_users_posts() 参数

历史

  • 3.5.0: 添加 public_only parameter
  • 添加于 版本: 3.0.0

count_many_users_posts() 历史

源文件

count_many_users_posts() 函数的代码位于 wp-includes/user.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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;
}

//
// User option functions
//

count_many_users_posts() 源文件

相关