返回介绍

count_user_posts()

发布于 2017-09-10 21:57:37 字数 4139 浏览 1274 评论 0 收藏 0

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

Number of posts user has written.


description


参数

$userid

(int) (Required) User ID.

$post_type

(array|string) (Optional) Single post type or array of post types to count the number of posts for.

Default value: 'post'

$public_only

(bool) (Optional) Whether to only return counts for public posts.

Default value: false


返回值

(string) Number of posts the user has written in this post type.


源代码

File: wp-includes/user.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 );
}

更新日志

Versiondescription
4.3.0Added $public_only argument. Added the ability to pass an array of post types to $post_type.
4.1.0Added $post_type argument.
3.0.0Introduced.

相关函数

Uses

  • wp-includes/plugin.php: apply_filters()
  • wp-includes/user.php: get_usernumposts
  • wp-includes/post.php: get_posts_by_author_sql()
  • wp-includes/wp-db.php: wpdb::get_var()

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::get_item_permissions_check()
  • wp-includes/deprecated.php: get_usernumposts()
  • wp-includes/author-template.php: get_the_author_posts()

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

    Get post count for a user
    Display the number of posts published by the user with an ID of 5.

    
    <?php printf( __( 'Number of posts published by user: %s', 'textdomain' ), count_user_posts( 5 ) ); ?>
    
  2. Get post count for a user filtered by post type
    Display the number of posts of post type “book” published by the user with an ID of 5.

    
    <?php printf( __( 'Number of posts published by user: %s', 'textdomain' ),  count_user_posts( 5 , "book"  ) ); ?>
    

    Translation friendly post count
    The same operation, with translation support.

    
    <?php printf( __( 'Number of posts published by user: %d', 'wpdocs_textdomain' ), count_user_posts( 5 ) ); ?>
    

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

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

发布评论

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