外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>WordPress开发>WordPress函数:count_user_posts 获取用户文章数

WordPress函数:count_user_posts 获取用户文章数

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/

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

WordPress 3.9+ TinyMCE 4 增强:添加样式、按钮、字体、下拉菜单和弹出式窗口

2014-11-22 10:15:15

WordPress开发

创建你的第一个WordPress小工具

2015-1-22 9:47:07

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索