當(dāng)前位置:首頁(yè)>WordPress建站>WordPress開發(fā)>WordPress函數(shù):count_user_posts 獲取用戶文章數(shù)

WordPress函數(shù):count_user_posts 獲取用戶文章數(shù)

count_user_posts 是一個(gè)用來(lái)獲取用戶文章數(shù)量的WordPress函數(shù),該函數(shù)位于 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 開始添加了一個(gè)文章類型參數(shù),具體用法如下:

count_user_posts( $userid, $post_type )

參數(shù):

$userid 用戶的ID,必填

$post_type 文章類型,默認(rèn)為 post,選填

比如我調(diào)用 ID 為 23 的這個(gè)用戶的 question 這個(gè)文章類型的數(shù)量:

<?php echo count_user_posts( 23, 'question' ) ?>

更多說(shuō)明請(qǐng)看:https://developer.wordpress.org/reference/functions/count_user_posts/

聲明:本站所有文章,如無(wú)特殊說(shuō)明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個(gè)人或組織,在未征得本站同意時(shí),禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺(tái)。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號(hào) WPDAXUE
WordPress開發(fā)

WordPress 3.9+ TinyMCE 4 增強(qiáng):添加樣式、按鈕、字體、下拉菜單和彈出式窗口

2014-11-22 10:15:15

WordPress開發(fā)

創(chuàng)建你的第一個(gè)WordPress小工具

2015-1-22 9:47:07

0 條回復(fù) A文章作者 M管理員
    暫無(wú)討論,說(shuō)說(shuō)你的看法吧
?
個(gè)人中心
購(gòu)物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

陵川县| 都兰县| 黄浦区| 大宁县| 会理县| 五原县| 永泰县| 佛冈县| 隆回县| 临泽县| 格尔木市| 皮山县| 桃园市| 泰来县| 万年县| 郓城县| 河源市| 象州县| 石门县| 鄱阳县| 蛟河市| 凤阳县| 桐庐县| 阳西县| 乌拉特后旗| 子长县| 武鸣县| 宜昌市| 通化市| 汶上县| 枝江市| 竹北市| 星子县| 青海省| 苗栗县| 大悟县| 邢台县| 万盛区| 肥城市| 鄯善县| 荣成市|