用户撰写的帖子数量。

原型

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

参数

$userid (int) (Required) 用户身份。

$post_type (array|string) (Optional)  单个帖子类型或帖子类型数组,用于计算帖子数量。

$public_only (bool) (Optional)  是否仅返回公共职位的计数。

返回值

(string)  用户在此帖子类型中撰写的帖子数。

源文件

路径:wp-includes/user.php

<?php
...
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" );
	/**
	 * Filters the number of posts a user has written.
	 *
	 * @since 2.7.0
	 * @since 4.1.0 Added `$post_type` argument.
	 * @since 4.3.1 Added `$public_only` argument.
	 *
	 * @param int          $count       The user's post count.
	 * @param int          $userid      User ID.
	 * @param string|array $post_type   Single post type or array of 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, $public_only );
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/count_user_posts/