WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 2 down vote accepted

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>";
share|improve this answer
Pretty cool! I didn't want to list all the post titles so I just took that line out but the loop is needed to add up all the views right? Is this going to be expensive if the user has 1,000 posts? – Pollux Khafra May 12 '12 at 14:24
simply test it with timer_start() and timer_stop() as it depends on your setup. – kaiser May 12 '12 at 15:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.