最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom field - update_post_meta

programmeradmin1浏览0评论

I am trying to update a custom field for a custom post type with this function php

add_action('wp_ajax_updatemeta', 'toggle_task_status');
add_action('wp_ajax_nopriv_updatemeta', 'toggle_task_status');
function toggle_task_status () {
    $post_id = $data['post_id'];
    return update_post_meta($post_id , 'task_status', 'open');
}

javascript


jQuery("form").on("submit", function () {
    console.log("hello");
    var post_id = jQuery("#post_id").val();
    console.log(post_id);
    var post_data = {
                    action: "updatemeta",
                    data: {
                        post_id: post_id,
                    }
                };
    jQuery.ajax({
        type: "POST",
        url: ".php",
        data: post_data,
        dataType: "json",
        cache: false,
        error: function (jqHXR, textStatus, errorThrown) {
            console.error("it is not working   "+textStatus, errorThrown);
        },
        success: function () {
            console.log("it is working");
        }
    
    })
})

and the code gives me success but it doesn't work , I see the same before and after the request

I am trying to update a custom field for a custom post type with this function php

add_action('wp_ajax_updatemeta', 'toggle_task_status');
add_action('wp_ajax_nopriv_updatemeta', 'toggle_task_status');
function toggle_task_status () {
    $post_id = $data['post_id'];
    return update_post_meta($post_id , 'task_status', 'open');
}

javascript


jQuery("form").on("submit", function () {
    console.log("hello");
    var post_id = jQuery("#post_id").val();
    console.log(post_id);
    var post_data = {
                    action: "updatemeta",
                    data: {
                        post_id: post_id,
                    }
                };
    jQuery.ajax({
        type: "POST",
        url: "http://cloud-accounting.com/wp-admin/admin-ajax.php",
        data: post_data,
        dataType: "json",
        cache: false,
        error: function (jqHXR, textStatus, errorThrown) {
            console.error("it is not working   "+textStatus, errorThrown);
        },
        success: function () {
            console.log("it is working");
        }
    
    })
})

and the code gives me success but it doesn't work , I see the same before and after the request

Share Improve this question asked Feb 28, 2022 at 12:22 Ahmed ZidanAhmed Zidan 32 bronze badges 1
  • Ypu're not passing $data to toggle_task_status() function – Abdul Awal Uzzal Commented Feb 28, 2022 at 12:40
Add a comment  | 

1 Answer 1

Reset to default 0

I don't think you're actually getting the post ID sent by the javascript.

function toggle_task_status () {
    $post_id = $_POST['data']['post_id'];
    $update = update_post_meta($post_id , 'task_status', 'open');
    if($update){
        wp_send_json('success');
    } else {
        wp_send_json('fail');
    }
    wp_die();
}

and then in your javascript

success: function (response) {
    console.log(response); // should be either 'success'  or 'fail'
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论