WordPress函数文档count_user_posts()
获取用户发布的文章数 描述 Returns the post count for a user. 用法 <…
获取用户发布的文章数
描述
Returns the post count for a user.
用法
<?php $user_post_count = count_user_posts( $userid , $post_type ); ?>
参数
$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”
示例
Get post count for a user
Display the number of posts published by the user with an ID of 5.
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?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.
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?php echo ‘Number of posts published by user: ‘ . count_user_posts( 5 , “book” ); ?>
|
Translation friendly post count
The same operation, with translation support.
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?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
历史
- 添加于 版本: 3.0.0
- 添加于 版本: 4.1.0 (Added post type support)
源文件
count_user_posts() 函数的代码位于 wp-includes/user.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* 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_many_users_posts,
get_posts_by_author_sql
- 原文:http://codex.wordpress.org/Function_Reference/count_user_posts
类别:WordPress函数文档、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!