Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

up vote 1 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

 
discard

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.