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?
Answer
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$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>"; |


