Problem:
You have a Post type, and added to it a Custom Field (numeric) and want to display a sum of all those fields in a View.
Solution:
A example code, fully adapted for a query on WordPress default Post Type and a Custom Numeric Field called "numeric-one"
function calc_func( $atts ){ // get all Posts of your type $all_posts = get_posts(array( 'numberposts' => -1, 'post_type' => 'post', ) ); //if it returns some posts if( $all_posts ){ //Start the count on 0 $single_posts_value_sum = 0; //now get the single posts fields values foreach( $all_posts as $single_post ){ //get each Posts post data $single_post_data = get_post($single_post); //get each ID $single_post_id = $single_post_data->ID; //get each posts field value $single_post_value = get_post_meta($single_post_id, 'wpcf-numeric-one', true); //we need to sum this up BEFORE the if is closed and BEFORE the foreach is closed //Sum the values all posts fields $single_posts_value_sum+= $single_post_value; } } return $single_posts_value_sum; //return summed value } add_shortcode( 'calc-total', 'calc_func' );
If you then use the ShortCode [calc-total] anywhere in your Site, you will see a total calculated from all fields "numeric-one" added to the Post Type "post"
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | - | 14:00 – 20:00 | 14:00 – 20:00 | 14:00 – 20:00 | 14:00 – 20:00 | 14:00 – 20:00 |
- | - | - | - | - | - | - |
Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)
This topic contains 10 réponses, has 5 voix.
Last updated by Alan Il y a 8 années et 3 mois.
Assisted by: Beda.