I have the below code (edited to reduce size) that is not working as intended. The update_post_meta is not updating. It does nothing if I have it the way it is, but if I uncomment the last 2 lines and print out the results, followed by die;, then the function works and update_post_meta works, and updates the custom field.
Any ideas??
function swd_add_time_as_line_item($post_id) {
$invoice_id = $post_id;
$title = $_POST['time_tracker_add_title'];
$description = $_POST['time_tracker_add_desc'];
$qty = '1';
// store the data in array
$arr = array("swd_billing_qty" => $qty, "swd_billing_title" => $title, "swd_billing_description" => $description, "swd_billing_amount" => '75' );
// get existing tasks, if they exist. will simply be empty if none exist
$existing_items = get_post_meta($invoice_id, 'swd_billing_item', true);
// add the array data to existing data, or simply adds it if there is none
$existing_items[] = $arr;
// update the post meta
update_post_meta($invoice_id, "swd_billing_item", $existing_items);
// pp($existing_items);
// die;
}
add_action('save_post', 'swd_add_time_as_line_item', 10, 1);
SOLVED I added the following code to the bottom of the function and it is now working.
$update = update_post_meta($invoice_id, "swd_billing_item", $the_items, $existing_items);
if($update == true) {
wp_safe_redirect('post.php?post=' . $invoice_id . '&action=edit&message=4');
die;
}