PHP SUM using post Custom Field

Question

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'){
    
    $Work_Required_Hours=$_POST['Work_Required_Hours'];
    $Parent_Work=$_POST['Work_Parent_TaskGroup'];
    $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);
            wp_update_post(array(
                                        '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.

and

$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;
0
Ali Hamdan 2 months 0 Answers 17 views 0

Leave an answer