^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数count_user_posts()用法示例

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

Number of posts user has written.


描述


参数

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

更新日志

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

相关函数

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 ) ); ?>
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈