我在functions.php中构建了一个自定义帖子,可以在admin中正常工作。现在我想使用ajax从另一个php文件手动创建自定义帖子。在functions.php中我得到了这样的东西:
add_action('save_post', 'save_reference');
function save_reference(){
global $post;
update_post_meta($post->ID, "company", $_POST["company"]);
update_post_meta($post->ID, "contact", $_POST["contact"]);
update_post_meta($post->ID, "address", $_POST["address"]);
update_post_meta($post->ID, "zipcode", $_POST["zipcode"]);
update_post_meta($post->ID, "town", $_POST["town"]);
update_post_meta($post->ID, "phone", $_POST["phone"]);
update_post_meta($post->ID, "mobile", $_POST["mobile"]);
update_post_meta($post->ID, "email", $_POST["email"]);
global $wpdb;
$wpdb->query("UPDATE $wpdb->posts
SET post_title = '$post->ID'
WHERE ID = $post->ID");
}
那个用post_metas设置保存帖子,然后将帖子标题更新为实际的post_id。
我已经设置了ajax工作,此时我手动设置所有$ _POST值。在我的ajax中(在functions.php中设置较低)我尝试调用save_reference();但没有任何反应。我有这样的事情:
$_POST['company'] = $json_array2[0];
$_POST['contact'] = $json_array2[1];
$_POST['address'] = $json_array2[2];
$_POST['zipcode'] = $json_array2[3];
$_POST['town'] = $json_array2[4];
$_POST['phone'] = $json_array2[5];
$_POST['mobile'] = $json_array2[6];
$_POST['email'] = $json_array2[7];
$_POST['post_type'] = 'reference';
$_POST['content'] = 'test content';
save_reference();
我不想用wp_query保存数据,因为我希望能够调用save_reference();因为它应该做的伎俩。我尝试了wp_insert,但没有成功。请帮帮我。
编辑我的解决方案: 在我使用的ajax中:
global $post;
$post_ID = wp_insert_post(array('post_type' => 'reference', 'post_status' => 'publish', 'post_content' => "$_POST[content]"));
$post = get_post($post_ID);
save_reference();
答案 0 :(得分:0)
以下示例修复了示例代码的一些问题。问题:
post_title
方向来绕过操作并过滤挂钩$_POST
通常不赞成$_POST
。这是一个与WordPress'建议。
// The save_post action will only pass in a $post_id, so we know it was AJAX if $json!=false
function save_reference( $post_id, $json=false ){
//verify post is not a revision
if ( $parent_id = wp_is_post_revision( $post_id ) )
$post_id = $parent_id;
// post we just saved
$post = get_post( $post_id );
// only update the post if we need to since it's expensive
if ( $post->post_title != $post_id ){
// since we are going to update this post, we have to unhook to prevent a loop
remove_action( 'save_post', 'save_reference' );
// the data we want to update, set the post_title to the ID
$update = array(
'ID' => $post_id,
'post_title' => $post_id,
);
wp_update_post( $update );
// rehook since we DO want this method to be called if something else updates
add_action( 'save_post', 'save_reference' );
}
// since we gave the $json key values we can use it in place of $_POST
$input = ( $json )? $json : $_POST;
update_post_meta( $post_id, "company", $input["company"] );
update_post_meta( $post_id, "contact", $input["contact"] );
update_post_meta( $post_id, "address", $input["address"] );
update_post_meta( $post_id, "zipcode", $input["zipcode"] );
update_post_meta( $post_id, "town", $input["town"] );
update_post_meta( $post_id, "phone", $input["phone"] );
update_post_meta( $post_id, "mobile", $input["mobile"] );
update_post_meta( $post_id, "email", $input["email"] );
}
add_action( 'save_post', 'save_reference' );
以下是对save_reference()
函数的调用从您的AJAX中看起来的样子
// This could be done elsewhere if use a key instead if integer index for the array
$json = array(
'company' => $json_array2[0],
'contact' => $json_array2[1],
'address' => $json_array2[2],
'zipcode' => $json_array2[3],
'town' => $json_array2[4],
'phone' => $json_array2[5],
'mobile' => $json_array2[6],
'email' => $json_array2[7],
'post_type' => 'reference', // not sure what this is for?
'content' => 'test content', // not sure what this is for?
);
$post_id = // ??? you need to pass in a post_id with your AJAX call
save_reference( $post_id, $json );