Hi Jared,
I was wondering if you could help me out as well with a similar issue. I need to calculate the sum of an ACF number field in a CPT and show it in the front end with a shortcode… but I am not a programmer and don’t know how to write the code for it. I tried rewriting the code shown here – https://support.advancedcustomfields.com/forums/topic/calculating-a-sum-of-a-number-field-for-all-posts/ – with my own CPT and field slugs, etc, but it’s not working.
The custom post type is ‘investment_project’ and the field is ‘dashboard_value’. How do I create a function that will add all the number values in the Dashboard value field and show it through a shortcode?
All your help will be greatly appreciated!!!
Thank you
Hi Laurent,
I just saw your post about calculating the sum of a number field for a CPT and was wondering if you got it to work and if you could please share the code with me. I am not a programmer and am having a hard time figuring it out.
My custom post type is ‘investment_project’
Number field is ‘dashboard_value’
I need to show the sum of ‘dashboard_value’ through a shortcode in my home page.
Therefore, my code should look like this?:
function get_total_investment_value($query_args) {
$total = 0;
$posts = get_posts($query_args=’-1′);
foreach ($posts as $post) {
$total += get_field(‘dashboard_value’, $post->ID);
}
return $total;
}
function show_total_investment_value() {
print get_total_investment_value(array(‘post_type’=>’investment_project’));
}
add_shortcode(‘show_the_total_investment_value’, ‘show_total_investment_value’);
And do I just add this directly as is in the functions.php of my child theme?
Any help at all will be greatly appreciated!
Thank you