count_user_posts 是一个用来获取用户文章数量的WordPress函数,该函数位于 wp-includes/user.php 文件,具体代码如下:
function count_user_posts( $userid, $post_type = 'post' ) { global $wpdb; $where = get_posts_by_author_sql( $post_type, true, $userid ); $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. * * @param int $count The user's post count. * @param int $userid User ID. * @param string $post_type Post type to count the number of posts for. */ return apply_filters( 'get_usernumposts', $count, $userid, $post_type ); } |
从 4.1 开始添加了一个文章类型参数,具体用法如下:
count_user_posts( $userid, $post_type ) |
参数:
$userid 用户的ID,必填
$post_type 文章类型,默认为 post,选填
比如我调用 ID 为 23 的这个用户的 question 这个文章类型的数量:
<?php echo count_user_posts( 23, 'question' ) ?> |
更多说明请看:https://developer.wordpress.org/reference/functions/count_user_posts/
来源:
https://www.wpdaxue.com/count_user_posts.html