1

I have a page where I'm calling event titles based on the current user. I'm calling them into a table and associating a 'credit' with each event.

The table gets populated with the events and credits. At the bottom of the page at the moment I'm just bringing in the credits from each event, but I'd like to add up all the relevant event credits. Is there a way to do this?

$current_user = get_current_user_id();  
?><h2>Completed Courses</h2><?php

$post_args = array(
    'post_type'      => 'tribe_events',
//  'paged'          => $paged,
    'eventDisplay'   => 'past',
//  'date_query'     => array( array( 'after' => '-1 year' ) ),
//  'posts_per_page' => 20,
//  'cat'            => '7',
    'tax_query'      => array(
        array(
            'taxonomy' => 'tribe_events_cat',
            'field'    => 'slug',
            'terms'    => 'business-skills-personal-and-commercial',
        ),
    ),
    'meta_query' => array(
        array(
            'key'     => 'associated_people',
            'value'   => $current_user,
            'compare' => 'LIKE'
        )
    )
); 
$post_list = new wp_query( $post_args ); 
?>                          
<table style="width:100%">      
    <tr>
        <td>Business Skills (Personal and Commercial)</td>
    </tr>                                           
    <?php
    if ( $post_list->have_posts() ) : 
        while( $post_list->have_posts() ) : 
            $post_list->the_post();
            ?>                                                      
            <tr>
                <td><?php the_title(); ?></td>
                <td><?php the_field( 'cpd_credits' ); ?></td>
                <td>View Notes</td>
            </tr>                                   
            <?php 
        endwhile; 
    else : 
    endif; 
    wp_reset_query(); 
    ?>                                              
    <tr>
        <td>Total</td>
    </tr>
</table>

<?php 
$post_list = new wp_query( $post_args ); 
?>                              
<table style="width:100%">                                                                                                  
    <?php
    if ( $post_list->have_posts() ) : 
        while( $post_list->have_posts() ) : 
            $post_list->the_post();
            the_field( 'cpd_credits' );
        endwhile; 
    else : 
    endif; 
    wp_reset_query(); 
    ?>                                              
    <tr>
        <td>Total</td>
    </tr>
</table>

I attempted this, but no luck:

$post_list = new wp_query( $post_args ); 
?><table style="width:100%"><?php
if ( $post_list->have_posts() ) : 
    while( $post_list->have_posts() ) : 
        $post_list->the_post();
        // First declare total and count before the loop
        $total = 0;
        $count = 0;
        foreach( $posts as $post ) {
            if ( get_field( 'cpd_credits' ) ) { 
                // If we have a value add it to the total and count it
                $total += get_field( 'cpd_credits' );
                $count++;
            }
        }
        echo 'Count: '. $count;
        echo 'Total Sum: '. $total;
    endwhile; 
else :
endif; 
wp_reset_query();
1

ACF uses get_field so you can get the value before you output it. Just make sure you have a $total_credit variable before you start the loop and just add to it. When the loop is done print the total to your table.

NOTE: This is just pseudo code, so don't copy/paste this, just use it as a reference on how to add the value then output the total.

<?php 

// Define the total credits
$total_credit = 0; 

$post_list = new wp_query( $post_args ); ?>
<table style="width:100%"><?php
 if( $post_list->have_posts() ) : while( $post_list->have_posts() ) : $post_list->the_post();

// Get the current credits
$cur_credit = get_field('cpd_credits');

 // Add to the total credits
$total_credit += $cur_credit;

<?php endwhile; else : ?>
<?php endif; wp_reset_query(); ?>

    <tr>
        <td>Total: <?php 
            // Print the final total
            echo $total_credit; 
            ?></td>
    </tr>

</table>
  • Awesome! That's good to hear. – jgraup Dec 9 '15 at 9:43
0

Create a function what iterates through all the posts what are relevant. While iterating, get the credit field and add it to a counter variable. The last thing you've to do is return the counter variable what should've stored the total amount of credits for those posts.

Your code probably will look like this

in the file included before your view is included, e.g. functions.php

function get_total_credit($query_args) {
    $total = 0;
    $posts = get_posts($query_args);
    foreach ($posts as $post) {
        $total += get_field('credit', $post->ID);
    }
    return $total;
}

in your view

print get_total_credit(array('post_type'=>'page', ...));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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