1

Long time user, first time poster. The forums here have got me out of a lot of trouble but I'm really stuck on this one.

I'm using Pods for a Custom Post Type that has a Custom Field where a user can enter a numeric value. eg. or 4 or 2 etc

I want to display the total sum of this Custom Field across all user made posts on the front end. To achieve this I'm using a Pods Template to make a short code for the front end, but to do the calculation I'm using PHP.

So my current PHP is:

function jobs_total ($id) {
    $pods = pods ('pledged_job', $id);
    $jobs = ($pods->field ('jobs_pledged'));
    $a = ($jobs);
    $b = explode(' ', $a);

var_dump($b);
}

And the result I get so far is:

array(1) { [0]=> string(1) "5" } 
array(1) { [0]=> string(1) "4" } 
array(1) { [0]=> string(1) "2" } 
array(1) { [0]=> string(1) "7" }

How do I take the numeric values from "_", which are correctly appearing from post entries, and combine them in a new array so I can perform an 'array_sum' and return the total of those numbers?!

I'm a PHP novice so I'm not sure if this is obvious or if it's a clash between Pods terms and standard PHP.

Thanks in advance!!

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
  • 1
    It looks like `jobs_total` is getting called 4 separate times, right? What you need to do is edit this function to return one of the values you want to sum, and then wherever this function is called, add these values to an array (or just keep a running total). Then you can use `array_sum` to add up all of the numbers at once. Before writing out a potential answer, could you show us an example of what `$a` is (or what you expect it to be)? I'm curious what the purpose of `explode` is in this context, because it might be enough to return `$jobs` or something instead. – WOUNDEDStevenJones May 01 '20 at 02:32
  • `How do I take the numeric values from "_"` not sure what you're asking here. Are you talking about the `explode`? Showing what the values of `$pods`, `$jobs`, and `$a` are will help us debug this further. – WOUNDEDStevenJones May 01 '20 at 02:36
  • Thanks for having a look @WOUNDEDStevenJones! `echo ($pods);` returns `post_type:pledged_jobpost_type:pledged_jobpost_type:pledged_jobpost_type:pledged_job` `echo ($jobs);` returns `5427`, which is the numbers I'd like to total ultimately. `echo ($a);` returns `5427` as well :/ I had read that `explode` should return a single array so I was trying to use that on `$a` in an attempt to combine the four arrays. That's a great point that the actual function might be getting called 4 separate times, there are 4 user submitted posts I'm testing with. – Jonah Booth-Remmers May 01 '20 at 02:52
  • Regarding `numeric values`, I was hoping to get the numbers '5427, out of the individual arrays and into a new one. But your suggestion on the function being called 4x sounds like it might be the better way to go – Jonah Booth-Remmers May 01 '20 at 02:54
  • Take a look at some examples on https://www.php.net/manual/en/function.explode.php and https://www.php.net/manual/en/function.array-sum.php to get a better sense of what they can be used for. It looks like you're close already. Is `$jobs` here a string of 4 digits `5427` or is it an array of some sort? Getting a better sense of what the data actually is (`var_dump` gives a more clear result compared to `echo`). – WOUNDEDStevenJones May 01 '20 at 03:50
  • Thanks for the resources, I'll check them out. I do feel like I'm very close, I have the right numbers being pulled in from the submitted posts, it's only a matter of adding them together as a single total sum of 18 in this case. As users submit and add more jobs this number should increase. So `var_dump ($jobs);` returns `string(1) "5" string(1) "4" string(1) "2" string(1) "7"`. Also `var_dump ($a);` returns the same result. And `var_dump ($b);` returns `array(1) { [0]=> string(1) "5" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "7" }`. – Jonah Booth-Remmers May 01 '20 at 04:01
  • You can remove the `$a` and `$b` lines from your function. Then at the bottom of this function `return intval($jobs);` to get the individual numbers. Then in the function that's calling `jobs_total`, keep a running sum like `$sum += jobs_total($id);` or add it to an array `$totals[] = jobs_total($id);` and then once you have all of the values you can `array_sum($totals);` for your final sum. – WOUNDEDStevenJones May 01 '20 at 13:19
  • 1
    Thanks so much for your help @WOUNDEDStevenJones! I was still having trouble getting a final sum, most likely because of my own inexperience with PHP and potentially the crossover with Pods. However, I have managed to solve my problem with the following thankfully `function jobs_shortcode () { $jobs = get_posts(array( 'post_type' => 'pledged_job', 'post_status' => 'publish', 'numberposts' => -1, )); $total = 0; foreach ($jobs as $field) { $total += (int) get_post_meta($field->ID, 'jobs_pledged', true); } echo $total; } add_shortcode( 'jobs', 'jobs_shortcode' );` – Jonah Booth-Remmers May 03 '20 at 05:23

1 Answers1

0

Final code wrapped in shortcode to allow for displaying on the frontend through Elementor

function jobs_shortcode () { 
$jobs = get_posts(array( 
'post_type' => 'pledged_job', 
'post_status' => 'publish', 
'numberposts' => -1, )); 

$total = 0; 

foreach ($jobs as $field) { 
$total += (int) get_post_meta($field->ID, 'jobs_pledged', true); 
} 

echo $total; 
} 
add_shortcode( 'jobs', 'jobs_shortcode' );