Title: Adding function to sum values in custom field
Last modified: August 30, 2016

---

# Adding function to sum values in custom field

 *  [plbarrett2004](https://wordpress.org/support/users/plbarrett2004/)
 * (@plbarrett2004)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/)
 * Hello,
 * I have a blog that records a time in hours for each post. This value is stored
   in a custom field called ‘session_duration’.
 * I am trying to sum all those values to get a total value. I’d like to store this
   function in the functions.php file with a shortcode so that I can call the total
   hours from a post or page.
 * Here is my current code:
 *     ```
       function total_hours (){
               $total_hours=0;
               $meta_key = 'session_duration';//set this to your custom field meta key
               $allhours=$wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
               foreach ($allhours as $hours) {
                       $total_hours = $total_hours + $hours;
               }
               echo 'Total hours are '.$total_hours;
       }
       add_shortcode('total_hours','total_hours');
       ```
   
 * However this causes multiple errors. If I remove this code from the function 
   and place it by itself it works. I’m a total newbie to php having only started
   working with it in the last week or so. Any help is appreciated.

Viewing 15 replies - 1 through 15 (of 25 total)

1 [2](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/page/2/?output_format=md)

 *  anonymized-13749270
 * (@anonymized-13749270)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6275712)
 * `$total_hours = strtotime($total_hours)+strtotime($hours);`
 * What are the values formats of `session_duration` in the postmeta table? like
   this 00:00:00 or?
 *  Thread Starter [plbarrett2004](https://wordpress.org/support/users/plbarrett2004/)
 * (@plbarrett2004)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6275806)
 * session_duration is a float. All values are one decimal precision. (Ie; 1.0, 
   5.5, etc) The number represents hours.
 *  Thread Starter [plbarrett2004](https://wordpress.org/support/users/plbarrett2004/)
 * (@plbarrett2004)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6275808)
 * If I put this portion of the above code into say single.php it works.
 *     ```
       total_hours=0;
               $meta_key = 'session_duration';//set this to your custom field meta key
               $allhours=$wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
               foreach ($allhours as $hours) {
                       $total_hours = $total_hours + $hours;
               }
               echo 'Total hours are '.$total_hours;
       ```
   
 *  anonymized-13749270
 * (@anonymized-13749270)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6275833)
 * You are missing [$wpdb](https://codex.wordpress.org/Class_Reference/wpdb) global
   variable from your code:
 * Try:
 *     ```
       function total_hours() {
       	global $wpdb;
           $total_hours=0;
           $meta_key = 'session_duration';//set this to your custom field meta key
           $allhours=$wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
           foreach ($allhours as $hours) {
                   $total_hours = $total_hours + $hours;
           }
           echo 'Total hours are '.$total_hours;
       }
       add_shortcode('total-hours','total_hours');
       ```
   
 * Use `[total-hours]`. I tested it, it worked ( returned Total hours are 7.8 when
   I had created meta values for key session_duration )..
 *  Thread Starter [plbarrett2004](https://wordpress.org/support/users/plbarrett2004/)
 * (@plbarrett2004)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6276039)
 * Thanks Sam. I am now able to get the code to work, but another error is occurring.
 * The sum is not matching the data. It seems that data is sometimes double counted,
   or if changed, not deleting / overwriting but appending.
 * Here is my code that has been added to functions.php in my twenty eleven theme.
 *     ```
       function total_hours(){
           global $wpdb;
           unset($hours);
           $hours = array();
           $meta_key = 'session_duration';//set this to your custom field meta key
           $hours = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
           return array_sum($hours);
       }
       add_shortcode('total-hours','total_hours’);
       ```
   
 * Here is an example of the performance I am seeing with the short code [total-
   hours]
 * 989.25 and then I add a custom field entry of 10, and save. The new total-hours
   = 1009.25. Now, I remove the 10 hours field entry, and the total hours becomes
   999.25. It double counts on the initial entry, but removes the correct amount.
   Meaning the total becomes more and more incorrect with each entry and or change.
 *  anonymized-13749270
 * (@anonymized-13749270)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6276040)
 * Can you do this:
 * `var_dump($hours);`
 * And then tell us what is in the output? I am just trying to understand how data
   are added in postmeta, so I could make myself an example to study..
 *  Thread Starter [plbarrett2004](https://wordpress.org/support/users/plbarrett2004/)
 * (@plbarrett2004)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6276046)
 * Well its a lot, hopefully I did it correctly.
 * Here’s the modified code:
 *     ```
       function total_hours(){
           global $wpdb;
           unset($hours);
           $hours = array();
           $meta_key = 'session_duration';//set this to your custom field meta key
           $hours = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
       /*   return array_sum($hours);*/
           return  var_dump($hours);
       }
       add_shortcode('total-hours','total_hours');
       ```
   
 * `array(376) { [0]=> string(1) "6" [1]=> string(2) "10" [2]=> string(1) "4" [3]
   => string(1) "4" [4]=> string(1) "3" [5]=> string(1) "4" [6]=> string(3) "2.5"[
   7]=> string(3) "2.5" [8]=> string(1) "3" [9]=> string(1) "3" [10]=> string(1)"
   0" [11]=> string(1) "0" [12]=> string(1) "0" [13]=> string(1) "0" [14]=> string(
   1) "0" [15]=> string(1) "0" [16]=> string(1) "2" [17]=> string(1) "2" [18]=> 
   string(1) "0" [19]=> string(1) "0" [20]=> string(1) "5" [21]=> string(1) "5" [
   22]=> string(1) "0" [23]=> string(1) "1" [24]=> string(1) "1" [25]=> string(1)"
   0" [26]=> string(1) "0" [27]=> string(1) "0" [28]=> string(1) "2" [29]=> string(
   1) "2" [30]=> string(1) "0" [31]=>`
 *  Thread Starter [plbarrett2004](https://wordpress.org/support/users/plbarrett2004/)
 * (@plbarrett2004)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6276047)
 * I only included the first part of the output, as there is quite a long output.
   Let me know if you need more output. I appreciate your help!!!
 * -Peter
 * Also my website is at [http://www.papalimabravo.com](http://www.papalimabravo.com).
 *  anonymized-13749270
 * (@anonymized-13749270)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6276048)
 * Hey Peter,
 * Thanks for the code. Can you give this a try:
 *     ```
       function total_hours(){
           global $wpdb;
           unset($hours);
           $hours = array();
           $meta_key = 'session_duration';//set this to your custom field meta key
           $hours = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
           $hour = 0;
           foreach ( $hours as $hour )
           	$sum += $hour;
       /*   return array_sum($hours);*/
           return  var_dump($sum);
       }
       add_shortcode('total-hours','total_hours');
       ```
   
 *  Thread Starter [plbarrett2004](https://wordpress.org/support/users/plbarrett2004/)
 * (@plbarrett2004)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6276049)
 * Notice: Undefined variable: sum in /var/www/papalimabravo.com/public_html/wp-
   content/themes/twentyeleven/functions.php on line 43
    float(1040.25)
 *  anonymized-13749270
 * (@anonymized-13749270)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6276050)
 *     ```
       $sum = '';
       function total_hours(){
           global $wpdb;
           unset($hours);
           $hours = array();
           $meta_key = 'session_duration';//set this to your custom field meta key
           $hours = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
           $hour = 0;
           foreach ( $hours as $hour )
           	$sum += $hour;
       /*   return array_sum($hours);*/
           return  var_dump($sum);
       }
       add_shortcode('total-hours','total_hours');
       ```
   
 *  Thread Starter [plbarrett2004](https://wordpress.org/support/users/plbarrett2004/)
 * (@plbarrett2004)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6276051)
 * I thought that would do it, but…
 * Notice: Undefined variable: sum in /var/www/papalimabravo.com/public_html/wp-
   content/themes/twentyeleven/functions.php on line 46
    float(1040.25)
 *  anonymized-13749270
 * (@anonymized-13749270)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6276052)
 * Wow again? It seems you added 3 lines..
 *     ```
       function total_hours() {
           global $wpdb;
           unset( $hours );
           $sum = 0;
           $hours = array();
           $meta_key = 'session_duration';//set this to your custom field meta key
           $hours = $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key ) );
           foreach ( $hours as $hour )
           	$sum += $hour;
       	/*return array_sum($hours);*/
           return  var_dump( $sum );
       }
       add_shortcode('total-hours','total_hours');
       ```
   
 * If the error still consists, then, there will be another play around as I saw
   the function outputted already an array of all hours..
 *  Thread Starter [plbarrett2004](https://wordpress.org/support/users/plbarrett2004/)
 * (@plbarrett2004)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6276053)
 * This returns:
    float(1040.25)
 * Now, that number still looks high, so I tested. Adding an entry of 10 results
   in
    float(1060.25)
 * Removing the entry results in
    float(1050.25)
 *  Thread Starter [plbarrett2004](https://wordpress.org/support/users/plbarrett2004/)
 * (@plbarrett2004)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/#post-6276054)
 * The extra lines are from me being sloppy with my code insert… added some extra
   line breaks to keep myself straight.

Viewing 15 replies - 1 through 15 (of 25 total)

1 [2](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/page/2/?output_format=md)

The topic ‘Adding function to sum values in custom field’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [sum](https://wordpress.org/support/topic-tag/sum/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 25 replies
 * 2 participants
 * Last reply from: anonymized-13749270
 * Last activity: [10 years, 11 months ago](https://wordpress.org/support/topic/adding-function-to-sum-values-in-custom-field/page/2/#post-6276065)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
