这是因为当您更新帖子时,使用了 *wp_insert_post* 函数,并且有“save_post”(1) 操作挂钩,通常用于保存自定义字段数据。
添加/更新帖子元的标准方法是这样的:
$post_meta['_mymeta'] = $_POST['_mymeta'];
// Add values of $events_meta as custom fields
foreach ($events_meta as $key => $value) { // Cycle through the $post_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
if($value && $value != get_post_meta($post->ID, $key, TRUE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} elseif($value && get_post_meta($post_id, $key, TRUE) == "") { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value, TRUE);
}
if(!$value) delete_post_meta($post->ID, $key, get_post_meta($post->ID, $key, TRUE)); // Delete if blank
}
...如您所见,它正在检查 *$_POST* 数据,如果它为空或未设置,它会使用空数据更新您的元值或将其完全删除。
我想您应该使用数据库更新功能或其他一些 API 功能来更新帖子字段...例如,这段代码将更新您的帖子菜单顺序:
$wpdb->update( $wpdb->posts, array( 'menu_order' => 5 ), array( 'ID' => $post->ID ) );
(1) 每当创建或更新帖子或页面时运行,这可能来自导入、帖子/页面编辑表单、xmlrpc 或通过电子邮件发布。动作函数参数:帖子 ID。