count_user_posts 是一个用来获取用户文章数量的WordPress函数,该函数位于 wp-includes/user.php 文件,具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 ); } |
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 开始添加了一个文章类型参数,具体用法如下:
1 | count_user_posts( $userid, $post_type ) |
count_user_posts( $userid, $post_type )
参数:
$userid 用户的ID,必填
$post_type 文章类型,默认为 post,选填
比如我调用 ID 为 23 的这个用户的 question 这个文章类型的数量:
1 | <?php echo count_user_posts( 23, 'question' ) ?> |
<?php echo count_user_posts( 23, 'question' ) ?>
更多说明请看:https://developer.wordpress.org/reference/functions/count_user_posts/
以上就是WordPress函数:count_user_posts 获取用户文章数。自私的人会为自己的行为做任何辩护,其中最常见的一种借口就是“我是为你好”。更多关于WordPress函数:count_user_posts 获取用户文章数请关注haodaima.com其它相关文章!