plbarrett2004
Forum Replies Created
-
Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldSamuel,
That works like a charm. Nice touch being able to pass the category id to the function via the short code! That makes building the site the way I want a snap. I’ve used this and a few other tweaks to build my index page dynamically in the format I want. This has been on my to-do list for months.
You’ve been a HUGE help. Thank you!!
Cheers,
PeterForum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldYes that’s correct. The category ID for the example is 3.
Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldDigging a little further into this, the wp_postmeta table does not have a column for the category, so I’d need to do more than one statement to first select only posts from that category…
Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldOk, so the problem was definitely the plugin. By using the default custom field entry ‘shop_hours’ in posts, I am able to get the appropriate behavior. The total is working properly. I finally cleaned up my code, and my next thought is to implement the same code by category. I don’t think I can use the current select statement to do so though… any thoughts?
/* The following functions will be used to total all session hours by post using the custom field 'shop_hours' Subsequent functions will work for specific categories. */ // Function totals all session hours function total_hours() { global $wpdb; $total_hours=0; $meta_key = 'shop_hours';//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; } return $total_hours; } // Total of all hours by category // Total hours for empennage (cat id = 3) function emp_hours() { global $wpdb; $emp_hours=0; $meta_key = 'shop_hours';//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) { $emp_hours = $emp_hours + $hours; } return $emp_hours; } // Shortcodes for all hour total functions add_shortcode('total-hours','total_hours'); // Total add_shortcode('emp-hours','emp_hours'); // Empennage (cat id = 3)Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldOk, I made some progress. I should have recognized the problem was on the input earlier. I was using a plugin called Custom Fields. If I used the built in wordpress custom fields, it now works, or at least it is outputting the proper array of numbers.
Previously every edit was appending a new entry to the array.
I’ll post results in a bit after I play some more.
Much appreciate your help!!!!
Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldThe extra lines are from me being sloppy with my code insert… added some extra line breaks to keep myself straight.
Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldThis 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)Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldI 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)Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldNotice: Undefined variable: sum in /var/www/papalimabravo.com/public_html/wp-content/themes/twentyeleven/functions.php on line 43
float(1040.25)Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldI 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.
Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldWell 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]=>Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldThanks 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.
Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldIf 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;Forum: Fixing WordPress
In reply to: Adding function to sum values in custom fieldsession_duration is a float. All values are one decimal precision. (Ie; 1.0, 5.5, etc) The number represents hours.