WordPress.org

Support

Support » How-To and Troubleshooting » Display Sum of meta values

Display Sum of meta values

  • I have a meta value for each post, named “revenue”.
    When I do a custom query to list all the posts and their revenue it works. However what I’m trying to do is to sum the value of all the “revenue” into one number.

    This is my query to list the revenue for each post

    <?php
     query_posts('category_name=active-project');
     while (have_posts()) : the_post();
    	 $revenue = get_post_meta($post->ID, 'revenue', true);
    	echo '<p>'.$revenue.'</p>';
    endwhile;
    ?>

    And this is what I get:

    <p>10.00</p>
    <p>15.00</p>
    <p>11.00</p>
    <p>15.00</p>

    What I’m trying to do is to sum all of this numbers and add the total amount right after the list.

Viewing 1 replies (of 1 total)
  • cdukes

    @walkinonwat3r

    UNTESTED

    <?php
    $revenue = 0;
    query_posts(‘category_name=active-project’);
    while (have_posts()) : the_post();
    $revenue .= get_post_meta($post->ID, ‘revenue’, true);
    endwhile;
    echo ‘<p>’.$revenue.'</p>’;
    ?>

    Might have to validate the metas as numbers, or use + instead of .=

Viewing 1 replies (of 1 total)
  • The topic ‘Display Sum of meta values’ is closed to new replies.
Skip to toolbar