最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to show total view count across all posts for an author

programmeradmin3浏览0评论

I'm using wp-postviews to track views for posts by authors on my site. It stores the view count in the post meta field $views. I'd want to show on their profile a total count of views for all their posts combined. How to do that?

I'm using wp-postviews to track views for posts by authors on my site. It stores the view count in the post meta field $views. I'd want to show on their profile a total count of views for all their posts combined. How to do that?

Share Improve this question asked May 12, 2012 at 13:28 Pollux KhafraPollux Khafra 8884 gold badges25 silver badges50 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

A list of posts in the format:

Post views by Author: Author Name

  • Post Title (15)
  • Post Title (67)
  • Post Title (4)

Total Number of views: 86

$author_id = ''; // do stuff to get user ID

$author_posts = get_posts( array(
    'author' => $author_id
) );

$counter = 0; // needed to collect the total sum of views

echo '<h3>Post views by Author:</h3><ul>'; // do stuff to get author name
foreach ( $author_posts as $post )
{
    $views = absint( get_post_meta( $post->ID, 'views', true ) );
    $counter += $views;
    echo "<li>{$post->post_title} ({$views})</li>";
}
echo "</ul><hr /><p>Total Number of views: <strong>{$counter}</strong></p>";
发布评论

评论列表(0)

  1. 暂无评论