WordPress.org

Ready to get started?Download WordPress

Forums

Display Sum of meta values (2 posts)

  1. artinh82
    Member
    Posted 1 year ago #

    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.

  2. cdukes
    Member
    Posted 1 year ago #

    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 .=

Topic Closed

This topic has been closed to new replies.

About this Topic