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

发布于 2024-10-31 00:46:46 字数 591 浏览 1 评论 0原文

我正在尝试通过 wp_update_post 函数更新我的一篇帖子中的帖子内容。我已阅读此处的文档: http://codex.wordpress.org/Function_Reference/wp_update_post

并且如果我做对了,我只需要发送帖子 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 );
}

为什么会发生这种情况以及如何解决它?

I'm trying to update the post content in one of my post through the wp_update_post function. I have read the documentation here: http://codex.wordpress.org/Function_Reference/wp_update_post

And if I get it right I just need to send the post ID and the post content I want to update with - just like in the example - and this should be the only thing that will change. Although my custom fields that I have attached to this post disappears, strange enough.

I have the following code that I pass on:

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 );
}

How come this happen and how do I solve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很快妥协 2024-11-07 00:46:46

这是因为当您更新帖子时,会使用 *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。

This is because when you are updating the post the *wp_insert_post* function is used and there is "save_post"(1) action hook which is usually used for saving custom fields data.

The standard way to add/update post meta is something like this:

$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
}

...as you can see it is checking for *$_POST* data and if it is empty or not set it updates your meta value with empty data or deletes it completely.

I suppose you should use database update function or some other API function to update post fields...for example this piece of code will update your post menu order:

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

(1) Runs whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email. Action function arguments: post ID.

裸钻 2024-11-07 00:46:46

为了避免这种行为,将第三个参数设置为 false。
它停用“插入后挂钩”。

To avoid that behavior set false as third parameter.
It deactivates the "after insert hooks".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文