How can i sum of values in wordpress from ACF field?

299 Views Asked by At

I have a query and i can't sum value in array.

My WP query

<?php
$income_query = new WP_Query(
                    array(
                        'post_type' => 'car',
                        'post_status' => 'publish',
                        'posts_per_page' => '-1',
                        'orderby' => 'date',
                        'date_query' => array(
                            'after' => 'today',
                            'before' => 'tomorrow - 1 second',
                            'inclusive' => true,
                        ),
                        // Set new values for relevant arguments.
                        'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
                    )
                ); ?>
                <?php
                if ($income_query->have_posts()): ?>
                <?php
                    while ($income_query->have_posts()):
                        $income_query->the_post(); ?>
                // Get field objects 
                <?php $field = get_field_object('recog-price');?>
               // Echo field value key 
               echo $field['value'];
<?php endwhile; ?>
                <?php endif; ?>

If i input echo $field['value']; display all values in value key from recog-price field.

I used array_sum and many ways to sum all prices from value key but doesn't work.

How can i do it ?

Update with var_dump($field)

array(27) 
{
     ["ID"]=> int(135) 
     ["key"]=> string(19) "field_6381cfae889e1" 
     ["label"]=> string(25) "مبلغ کارشناسی" 
     ["name"]=> string(11) "recog-price" 
     ["aria-label"]=> string(0) "" 
     ["prefix"]=> string(3) "acf" 
     ["type"]=> string(6) "number" 
      //I need sum this parametr in all my **car** custom posstyps
     **["value"]=> string(6) "200000"** 
     ["menu_order"]=> int(14) 
     ["instructions"]=> string(97) "لطفا مبلغ را به تومان و با کیبورد انگلیسی وارد نمایید." 
     ["required"]=> int(1) 
     ["id"]=> string(0) "" 
     ["class"]=> string(0) "" 
     ["conditional_logic"]=> int(0) 
     ["parent"]=> int(52) 
     ["wrapper"]=> array(3) 
     { 
        ["width"]=> string(2) "50" 
        ["class"]=> string(5) "price" 
        ["id"]=> string(0) "" 
    } 
    ["frontend_admin_display_mode"]=> string(4) "edit" 
    ["only_front"]=> int(0) 
    ["default_value"]=> int(0) 
    ["min"]=> string(0) "" 
    ["max"]=> string(0) "" 
    ["placeholder"]=> string(15) "مثلا 150000" 
    ["step"]=> string(0) "" 
    ["prepend"]=> string(0) "" 
    ["append"]=> string(12) "تومـان" 
    ["_name"]=> string(11) "recog-price" 
    ["_valid"]=> int(1) 
}

I need sum all values from this array .

1

There are 1 best solutions below

3
On

Thanks for sharing the Array.

You can try below simplify code:

Define $c = 0; before while loop;

if you have all the values as INT

// Echo field value key 
echo $c += (int)$field['value']; // Or you can use intval($field['value'])

if you have all the values as INT and FLOAT

// Echo field value key 
echo $c += (float)$field['value']; // Or you can use floatval($field['value'])

Please let me know if you find any issue with the above code.