count_user_posts()获取用户发布的文章数

目录

描述

Returns the post count for a user.

count_user_posts() 描述

用法

<?php $user_post_count count_user_posts$userid $post_type ); ?>

count_user_posts() 用法

参数

$userid

(integer) (必填) The ID of the user to count posts for.

默认值: None

$post_type

(string) (可选) Post type to count the number of posts for.

默认值: "post"

count_user_posts() 参数

示例

Get post count for a user

Display the number of posts published by the user with an ID of 5.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php echo 'Number of posts published by user: ' . count_user_posts( 5 ); ?>

Get post count for a user of post type

Display the number of posts of post type "book" published by the user with an ID of 5.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php echo 'Number of posts published by user: ' . count_user_posts( 5 , "book"  ); ?>

Translation friendly post count

The same operation, with translation support.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php printf( __( 'Number of posts published by user: %d', 'text-dom-here' ), count_user_posts( 5 ) ); ?>

Result

The result of the above two examples

Number of posts published by user: 123

count_user_posts() 示例

历史

  • 添加于 版本: 3.0.0
  • 添加于 版本: 4.1.0 (Added post type support)

count_user_posts() 历史

源文件

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

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

count_user_posts() 源文件

相关