wp_insert_post()返回Action失败。请刷新页面并重试

时间:2018-03-23 10:16:52

标签: php ajax wordpress

我正在尝试使用Wordpress插件中的wp_insert_post()创建一个新的Wordpress Post以进行AJAX调用。我已经检查过相关的参数是否适用于该函数但是作为我的响应我得到操作失败。请刷新页面并重试,这是wp_insert_post()

编辑:我已检查数据库以及数据库中正确存储的所有数据

我也尝试了wp_send_json( get_post(27538) ); die;,它在同一个函数中运行良好。

add_action( 'wp_ajax_tcf_et_create_new_estimate', 'tcf_et_create_new_estimate' );
function tcf_et_create_new_estimate()
{
    $project_name   = $_REQUEST['project_name'];
    $email_address  = $_REQUEST['email_address'];
    $ikea_system    = $_REQUEST['ikea_system'];
    $project_notes  = $_REQUEST['project_notes'];

    $user       = get_user_by( 'email', $email_address );
    $user_id    = is_int( $user->ID ) ? $user->ID : 1;

    $wishlist_data = array(
        'post_type'    => 'wishlist',
        'post_title'   => sanitize_text_field( $project_name ),
        'post_content' => $project_notes,
        'post_status'  => 'publish',
        'ping_status'  => 'closed',
        'post_excerpt' => '',
        'post_author'  => $user_id
    );

    // die('here'); --> THIS CODE WORKS AND IT RETUNS "here" AS RESPONCE IF UNCOMMENTED
    $webhook_id = wp_insert_post( $wishlist_data );
    wp_send_json( $webhook_id );
    die;
}

wp_insert_post()上面的代码有什么要传递的吗?

1 个答案:

答案 0 :(得分:0)

原因是我在注册'post_type' => 'wishlist'之前尝试添加wishlist