I tried explaining this yesterday but I fear I made things more complicated than they needed to be.

I have a custom loop that pulls through standard Wordpress posts with various arguments.

$args = array(
    'post_type' => 'post',
    'posts_per_page' => 50,
    'post__in' => array(2, 3, 7, 53, 75)

These posts all have a custom meta field called post_views which, based on a custom function, counts the page views of that particular post.

What I need to be able to do is combine the values of all these meta values and calculate the sum total that I can output into my theme.

So, for example;

  • If the 5 posts defined in the post__in argument each have 100 views, the sum total of all 5 post_views meta values would be 500.

I hope I have explained this a little better than my last attempt.

Any questions, please don't hesitate to ask.

Thanks in advance

  • This does make much more sense, and I think @birgire nailed it Commented Nov 24, 2015 at 9:48

1 Answer 1


I wonder if you mean this kind of loop:

$total_views = 0;
$post_ids    = [2, 3, 7, 53, 75];

foreach( $post_ids as $post_id )
    $total_views += (int) get_post_meta( $post_id, 'post_views', true );

to calculate the total sum.

  • That is how I read it as well. Commented Nov 24, 2015 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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