简体   繁体   中英

PHP and Pods: Combine array values only into new array

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!!

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' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM