wp_update_post使自定义字段值消失(WordPress的)

问题描述:

我想通过wp_update_post函数更新我的帖子之一的帖子内容。我已阅读这里的文档:http://codex.wordpress.org/Function_Reference/wp_update_postwp_update_post使自定义字段值消失(WordPress的)

如果我得到它的权利,我只需要发送帖子ID和我想更新的帖子内容 - 就像在示例中 - 这应该是唯一的会改变。尽管我附加到这篇文章中的自定义字段消失了,但还是很奇怪。

我有以下我传递代码:

if(isset($_POST['submit'])){ 
    $the_post = array(); 
    $the_post['ID'] = $_POST['id']; 
    $the_post['post_content'] = $_POST['recension']; 

    // Update the post into the database 
    wp_update_post($the_post); 
} 

怎么会出现这种情况,如何解决呢?

这是因为当您更新帖子时,会使用* 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 *数据,如果是空的,或者没有设置用空数据更新您的元值或完全删除它。

我想你应该使用database update function或其他一些API函数来更新后场......比如这段代码将更新后的菜单命令:

$wpdb->update($wpdb->posts, array('menu_order' => 5), array('ID' => $post->ID)); 

(1)运行时后或页面已创建或更新,可能来自导入,发布/页面编辑表单,xmlrpc或通过电子邮件发布。动作函数参数:帖子ID。

+0

我认为你的解决方案可能是我需要更新我现有的职位。我所做的是将变量的值反映到自定义字段中。面临的挑战是,该值显示在编辑屏幕中,但在更新帖子之前,不会在前端生效。我有1,800多个帖子,这需要很长时间才能手动更新。您的其中一个解决方案可以帮助我更新现有的帖子吗?虽然我不想删除空白字段。感谢您的帮助。 – gdinari 2011-06-22 21:12:54

+0

我希望函数在任何时候创建或导入帖子时运行,等等。请让我知道你是否有任何想法。谢谢。 – gdinari 2011-06-22 21:21:52