• 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)
  • anonymized-13749270

    (@anonymized-13749270)

    $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

    (@plbarrett2004)

    session_duration is a float. All values are one decimal precision. (Ie; 1.0, 5.5, etc) The number represents hours.

    Thread Starter plbarrett2004

    (@plbarrett2004)

    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)

    You are missing $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

    (@plbarrett2004)

    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)

    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

    (@plbarrett2004)

    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

    (@plbarrett2004)

    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.

    anonymized-13749270

    (@anonymized-13749270)

    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

    (@plbarrett2004)

    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)

    $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

    (@plbarrett2004)

    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)

    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

    (@plbarrett2004)

    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

    (@plbarrett2004)

    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)

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