1 官方函数

<?php $user_post_count = count_user_posts( $userid , $post_type ); ?>

函数参数:

  • $userid : 用户的ID,必填项
  • $post_type : 文章类型,默认为 post,选填

2 使用sql函数查询

function num_of_author_posts( $user_id ){
    global $wpdb;
    $user_id = (int) $user_id;
    $sql     = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_author='$user_id' AND post_status = 'publish' ";
    $coo     = $wpdb->get_var( $sql );
    return ( $coo ) ? $coo: 0;
}