1. 首页
  2. WordPress 函数手册

count_user_posts()

count_user_posts( int $userid, array|string $post_type = ‘post’, bool $public_only = false )

用户已写的帖子数。
Number of posts user has written.

目录锚点:#参数#返回#源码#笔记


参数(Parameters)

参数 类型 必填 说明
$userid (int) 必需 用户ID。
$post_type (array | string) 可选 要计算其文章数的单个文章类型或文章类型数组。
$public_only (bool) 可选 是否只返回公共职位的计数。

返回(Return)

(string)用户在此文章类型中写入的文章数。


源码(Source)

/**
 * Number of posts user has written.
 *
 * @since 3.0.0
 * @since 4.1.0 Added `$post_type` argument.
 * @since 4.3.0 Added `$public_only` argument. Added the ability to pass an array
 *              of post types to `$post_type`.
 *
 * @global wpdb $wpdb WordPress database object for queries.
 *
 * @param int          $userid      User ID.
 * @param array|string $post_type   Optional. Post type(s) to count the number of posts for. Default 'post'.
 * @param bool         $public_only Optional. Whether to only return counts for public posts. Default false.
 * @return int Number of posts the user has written in this post type.
 */
function count_user_posts( $userid, $post_type = 'post', $public_only = false ) {
	global $wpdb;

	$where = get_posts_by_author_sql( $post_type, true, $userid, $public_only );

	$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );

	/**
	 * Filter the number of posts a user has written.
	 *
	 * @since 2.7.0
	 * @since 4.1.0 Added `$post_type` argument.
	 * @since 4.3.0 Added `$public_only` argument.
	 *
	 * @param int          $count       The user's post count.
	 * @param int          $userid      User ID.
	 * @param string|array $post_types  Post types to count the number of posts for.
	 * @param bool         $public_only Whether to limit counted posts to public posts.
	 */
	return apply_filters( 'get_usernumposts', $count, $userid, $post_type );
}
更新版本 源码位置 使用 被使用
4.3.0 wp-includes/user.php:370 3 4

笔记(Notes)

获取用户的帖子计数
获取按投递类型筛选的用户的投递计数
翻译友好职位计数

count_user_posts() 为WP2原创文章,链接:https://www.wp2.cn/functions/count_user_posts/

发表评论

您的电子邮箱地址不会被公开。