萨龙网络开发的 MNews 主题中，集成了个人中心功能，需要获取文章、视频、下载、专题和商城的文章数量，这个是比较简单，使用`wp_count_posts()`函数即可。如果要获取不同用户、不同自定义字段下的不同文章类型的文章数量怎么办，同样很好解决，使用`get_posts()`函数。

### 一、代码

1. /\*不同用户，不同文章类型，不同字段下的文章数量\*/
2. function salong\_author\_post\_field\_count($post\_type,$user\_id,$field) {
3. global $post;
4. 
5. $post\_args = get\_posts( array(
6. ‘posts\_per\_page’ =&gt; -1,
7. ‘post\_type’ =&gt; $post\_type,
8. ‘post\_status’ =&gt; ‘publish’,
9. ‘author’ =&gt; $user\_id
10. ) );
11. 
12. $counter = 0;
13. 
14. foreach ( $post\_args as $post ){
15. $views = absint( get\_post\_meta( $post-&gt;ID, $field, true ) );
16. $counter += $views;
17. }
18. 
19. return $counter;
20. }

### 二、调用

调用很方便，比如要调用 ID 为1的用户，视频（video）类型文章下的有浏览量（views）的文章数量：

`echo salong_author_post_field_count('video','1','views');`