返回介绍

count_many_users_posts()

发布于 2017-09-10 21:57:04 字数 2785 浏览 1324 评论 0 收藏 0

count_many_users_posts( array $users,  string|array $post_type = 'post',  bool $public_only = false )

Number of posts written by a list of users.


description


参数

$users

(array) (Required) Array of user IDs.

$post_type

(string|array) (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


返回值

(array) Amount of posts each user has written.


源代码

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

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/post.php: get_posts_by_author_sql()
  • wp-includes/wp-db.php: wpdb::get_results()

Used By

  • wp-admin/includes/class-wp-users-list-table.php: WP_Users_List_Table::display_rows()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Returns the post counts for a list of user IDs.

    $users = array( 1, 3, 9, 10 );
    $counts = count_many_users_posts( $users );
    echo 'Posts made by user 3: ' . $counts[3];
    

    Output:

    Posts made by user 3: 143

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文