How to call update_post_meta?
-
I have a custom script for updating a custom post type. The calling script passes the post id of the post meta and populates a web page. The purpose of the page is to allow the user to update existing post meta data. The display aspect of the script works fine.?
I started using ajax for the update function. My problem is the code returns a success message yet nothing is actually updated. I do not know how to debug this.
Can you point out the flaws or omissions in my code?
Thank you!
————
Here is my code in that calls the update function
$(‘#update-time-entry’).click(function(event) {
var user_id = jQuery(‘#user_id’).val();
var user_name = jQuery(‘#user_name’).val();
var start_time = jQuery(‘#start_time’).val();
var end_time = jQuery(‘#end_time’).val();
var project_title = jQuery(‘#project-select’).val();
var project_description = jQuery(‘#project_desc’).val();
var payroll = jQuery(‘#payroll-select’).val();
Select allOpen in new window
var update_time_entry = new FormData();
update_time_entry.append(‘action’, ‘update_time_entry’);
update_time_entry.append(‘update_post_id’, update_post_id);
update_time_entry.append(‘user_id’, user_id);
update_time_entry.append(‘user_name’, user_name);
update_time_entry.append(‘start_time’, start_time);
update_time_entry.append(‘end_time’, end_time);
update_time_entry.append(‘project_title’, project_title);
update_time_entry.append(‘project_description’, project_description);
update_time_entry.append(‘payroll’, payroll);
jQuery.ajax({
type: “post”,
dataType: “json”,
url: “<?php echo admin_url(‘admin-ajax.php’); ?>”,
cache: false,
processData: false,
contentType: false,
enctype: ‘multipart/form-data’,
data: update_time_entry,
success: function(data) {
if (data.id == ‘success’) {
Here is the update function code
function update_time_entry() {
$update_post_id = sanitize_text_field($_POST[‘update_post_id’]);
$action = sanitize_text_field($_POST[‘action’]);
$user_id = sanitize_text_field($_POST[‘user_id’]);
$user_name = sanitize_text_field($_POST[‘user_name’]);
$start_time = sanitize_text_field($_POST[‘start_time’]);
$end_time = sanitize_text_field($_POST[‘end_time’]);
$project_title = sanitize_text_field($_POST[‘project_title’]);
$payroll = sanitize_text_field($_POST[‘payroll’]);
$project_description = sanitize_text_field($_POST[‘project_description’]);
// =============================CODE MODIFICATION========================
update_post_meta($update_post_id, ‘action’, $action);
update_post_meta($update_post_id, ‘user_id’, $user_id);
update_post_meta($update_post_id, ‘user_name’, $user_name);
update_post_meta($update_post_id, ‘start_time’, $start_time);
update_post_meta($update_post_id, ‘end_time’, $end_time);
update_post_meta($update_post_id, ‘project_title’, $project_title);
update_post_meta($update_post_id, ‘project_description’, $project_description);
update_post_meta($update_post_id, ‘payroll’, $payroll);
// =============================CODE MODIFICATION================================
echo json_encode(array(‘id’ => ‘success’));
exit();
}
- The topic ‘How to call update_post_meta?’ is closed to new replies.