How do i calculate the total of values of custom fields in custom post types?

So the title is a bit unclear, but here is what i have:

Custom post type: Projects
Custom post type: Investments
       + Custom field: Amount

Read More

And i have one page where i load the investments + amounts in a custom post type loop, so it looks like this:
Table:

____________________________________________
| custom post type 1 |      amount 1       |
|____________________|_____________________|
| custom post type 2 |      amount 2       |
|____________________|_____________________|

Now what i want to do is calculate the total amount of amount 1 + amount 2

btw, the code that i use is:

<?php
                    if ( is_user_logged_in() ):
                        global $current_user;
                        get_currentuserinfo();

                        $author_query = array('posts_per_page' => '-1','author' => $current_user->ID, 'post_type' => 'investeringen');
                        $author_posts = new WP_Query($author_query);
                        while($author_posts->have_posts()) : $author_posts->the_post();

                        $parent_id = wpcf_pr_post_get_belongs(get_the_ID(), 'project');
                        $parent = get_post($parent_id);
                        $percentagenumber = do_shortcode("[types field='rente' id='$parent_id']");

                        $geinvesteerd = types_render_field("geinvesterd", array( "raw" => "true") );
                        $opgebracht = $geinvesteerd / 100 * $percentagenumber; // 

                ?>
                <tr>
                    <td class="project">
                        <?php
                            $parent_id = wpcf_pr_post_get_belongs(get_the_ID(), 'project');
                                if (!empty($parent_id)) {
                                    $parent = get_post($parent_id);
                                    echo $parent->post_title;
                            }
                        ?>
                    </td>
                    <td class="demo">&euro; <?php echo number_format("$geinvesteerd",0,",","."); ?>,-</td>
                    <td>&euro; <?php echo number_format("$opgebracht",0,",","."); ?>,-</td>
                    <td>
                    <?php
                        echo number_format("$percentagenumber", 2, '.', '') . ' %'; 
                    ?>
                    </td>
                    <td><a href="
                    <?php
                        $parent_id = wpcf_pr_post_get_belongs(get_the_ID(), 'project');
                            if (!empty($parent_id)) {
                                $parent = get_post($parent_id);
                                echo post_permalink($parent_id);
                        }
                    ?>
                    "><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/meta_icons/eye.png" /></a></td>
                </tr>
            <?php
                global $count;
                $count = $author_posts->post_count;
                endwhile;

                else :
            ?>  
                <tr>
                    <td colspan="5" style="text-align:left">U moet inloggen voordat u deze pagina / statistieken kunt bekijken</td>
                </tr>
            <?php
                endif;
            ?>

Thanks for reading, hope you guys can help me out!

Related posts

1 comment

  1. It’s a matter of keeping a running total. Because I’m not 100% clear on the question, below I am providing the changes / pseudo-code for what you want to do:

    <?php
                    if ( is_user_logged_in() ):
                        global $current_user;
                        get_currentuserinfo();
    
                        // ADDED: Initialize the total variable to prevent warnings
                        $grandtotal = 0;
    
                        $author_query = array('posts_per_page' => '-1','author' => $current_user->ID, 'post_type' => 'investeringen');
                        $author_posts = new WP_Query($author_query);
                        while($author_posts->have_posts()) : $author_posts->the_post();
    
                        $parent_id = wpcf_pr_post_get_belongs(get_the_ID(), 'project');
                        $parent = get_post($parent_id);
                        $percentagenumber = do_shortcode("[types field='rente' id='$parent_id']");
    
                        $geinvesteerd = types_render_field("geinvesterd", array( "raw" => "true") );
                        $opgebracht = $geinvesteerd / 100 * $percentagenumber; //
    
                        // ADDED: Add this to the running total
                        $grandtotal+= $geinvesteerd; 
    
                ?>
                <tr>
                    <td class="project">
                        <?php
                            $parent_id = wpcf_pr_post_get_belongs(get_the_ID(), 'project');
                                if (!empty($parent_id)) {
                                    $parent = get_post($parent_id);
                                    echo $parent->post_title;
                            }
                        ?>
                    </td>
                    <td class="demo">&euro; <?php echo number_format("$geinvesteerd",0,",","."); ?>,-</td>
                    <td>&euro; <?php echo number_format("$opgebracht",0,",","."); ?>,-</td>
                    <td>
                    <?php
                        echo number_format("$percentagenumber", 2, '.', '') . ' &#37;'; 
                    ?>
                    </td>
                    <td><a href="
                    <?php
                        $parent_id = wpcf_pr_post_get_belongs(get_the_ID(), 'project');
                            if (!empty($parent_id)) {
                                $parent = get_post($parent_id);
                                echo post_permalink($parent_id);
                        }
                    ?>
                    "><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/meta_icons/eye.png" /></a></td>
                </tr>
            <?php
                global $count;
                $count = $author_posts->post_count;
                endwhile;
    
                // ADDED: Output the grand total here
                ?>
                <tr><td>TOTAL</td><td><?php echo number_format($grandtotal, 0, ",", "."); ?></td><td colspan="3">&nbsp;</td></tr>
                <?php
                else :
            ?>  
                <tr>
                    <td colspan="5" style="text-align:left">U moet inloggen voordat u deze pagina / statistieken kunt bekijken</td>
                </tr>
            <?php
                endif;
            ?>
    

Comments are closed.