PHP SUM using post Custom Field


I am trying to update a numeric custom field by ‘adding’ a number from a front-end form.
The script I wrote is not adding the existing number, but just updating to the new number entered in the form.

I haven’t been able to make it work but I think there needs to be some sort of conversion for the get_post_meta function for the script to understand it as a number.

The basic script I wrote is:

if(isset($_POST['Add_Work_Task']) == '1'){
    $Current_Buried_Hours =get_post_meta( $Parent_Work, 'Work_Buried_Hours',true);

    $Total_Buried_Hours= $Current_Buried_Hours + $Work_Required_Hours;

    $Buried_Required_Hours= array('Work_Buried_Hours' => $Total_Buried_Hours);
                                        'ID'        => $Parent_Work,
                                        'meta_input'=> $Buried_Required_Hours,

I also tried

$Total_Buried_Hours= (float)$Current_Buried_Hours + (float)$Work_Required_Hours;

$Total_Buried_Hours= floatval($Current_Buried_Hours) + floatval($Work_Required_Hours);

$Total_Buried_Hours= $Current_Buried_Hours += $Work_Required_Hours;

$Total_Buried_Hours= print_r($Current_Buried_Hours) + $Work_Required_Hours; this one adds 1 only.


$Current_Buried_Hours =implode(".",get_post_meta( $Parent_Work, 'Work_Buried_Hours',true));

To narrow it down I know for a fact the problem is with the get_post_meta function, I have tested adding the $_POST twice and it worked like the following:

$Total_Buried_Hours= $Work_Required_Hours + $Work_Required_Hours;
Ali Hamdan 2 months 0 Answers 17 views 0

Leave an answer