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)行處理。




