SQL sum of custom field split into months
-
Hi,
I have two parts to my question.1. I have Advanced Custom Fields installed and I am using that to build an accounting system for myself.
I’m working on the cashflow page and I need to show total money in per month and year automatically. eg. April 2015, Total money in is £100.
My custom field (number) ‘total_invoice_cost’ is what gets the figure (£100). I have another custom field (text) ‘paid’ which will have the options ‘yes’ ‘no’ and ‘overdue’
I need a way to show month by month the sum of invoices which have a figure inputted and are set to yes.
Here is what I have so far:
<?php $meta_key = 'total_invoice_cost'; $annualincome = $wpdb->get_var( $wpdb->prepare( " SELECT sum(meta_value) FROM $wpdb->postmeta WHERE meta_key = %s ", $meta_key ) ); echo "<p>Total money in this year is {$annualincome}</p>"; ?>
This shows the sum of all the ‘total_invoice_cost’ fields filled out and it isn’t broken up by month either.
So what needs to be done is only add if the custom field ‘paid’ is set to ‘yes’ and to also filter by month automatically.
2. How can I show the sum of ‘total_invoice_cost’ by itself for the current month? So the code above is nearly there for this part of the question, I just need it to show the total for the current month (and changing automatically) so I can keep track of money due in.
I hope this all makes sense, any help would be much appreciated.
- The topic ‘SQL sum of custom field split into months’ is closed to new replies.