【发布时间】:2019-01-23 12:25:45
【问题描述】:
我将高级自定义字段用于自定义帖子类型,以允许用户从前端添加帖子。我的更新代码工作正常。插入新帖子无法正常工作。它只是添加一个帖子,但没有保存任何数据。
我已经尝试了以下代码。
function my_acf_save_post( $post_id ) {
if( $post_id != 'new_post' ) {
// Get the selected post status
$value = get_field('post_status_field', $post_id);
// Update current post
$post = array(
'ID' => $post_id,
'post_status' => $value,
'post_title' => $_POST['acf']['_post_title'],
);
// Remove the action to avoid infinite loop
remove_action('acf/save_post', 'my_acf_save_post', 20);
// Update the post into the database
$post_id = wp_update_post( $post );
// Add the action back
do_action('acf/save_post', $post_id, 20);
}else{
// Get the selected post status
$value = get_field('post_status_field', $post_id);
// Update current post
$post = array(
'ID' => $post_id,
'post_status' => $value,
'post_title' => $_POST['acf']['_post_title'],
);
// Remove the action to avoid infinite loop
remove_action('acf/save_post', 'my_acf_save_post', 20);
// Update the post into the database
$post_id = wp_insert_post( $post );
// Add the action back
do_action('acf/save_post', $post_id, 20);
}
return $post_id;
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
我无法弄清楚我哪里出错了。我希望在提交帖子时提交所有数据,并且在更新时必须更新所有数据。
【问题讨论】: