描述
不必为草稿设置日期。您可以设置日期,并且不会被覆盖。
参数
$postarr
array|object可选-
文章数据。数组应该是转义的,对象不是。有关接受的参数,请参见wp_insert_post()。
默认数组。更多参数来自:wp_insert_post( ... $postarr )
构成要更新或插入的文章的元素数组。
ID
int文章ID。如果等于0以外的值,则将更新具有该ID的文章。默认值为0。post_author
int添加文章的用户的ID。默认值是当前用户ID。post_date
string文章的日期。默认值为当前时间。post_date_gmt
string在GMT时区中发布的日期。默认值为$post_date
。post_content
string文章内容。默认为空。post_content_filtered
string过滤后的文章内容。默认为空。post_title
string文章标题。默认为空。post_excerpt
string文章摘要。默认为空。post_status
string文章状态。默认'draft'
。post_type
string文章类型。默认'post'
。comment_status
string文章是否可以接受评论。接受'open'
或'closed'
。
Default是'default_comment_status'
选项的值。ping_status
string文章是否可以接受ping。接受'open'
或'closed'
。
默认值是'default_ping_status'
选项的值。post_password
string访问文章的密码。默认为空。post_name
string文章name。默认值是创建新文章时经过净化的文章标题。to_ping
string空格或回车符返回要ping的URL的分隔列表。
默认为空。pinged
string空格或回车符返回已Ping的URL的分隔列表。默认为空。post_modified
string上次修改文章的日期。默认值为当前时间。post_modified_gmt
string上次在GMT时区修改文章的日期。默认值为当前时间。post_parent
int为它所属的文章设置此项(如果有)。默认值为0。menu_order
int文章的显示顺序。默认值为0。post_mime_type
string文章的mime类型。默认为空。guid
string用于引用文章的全局唯一ID。默认为空。import_id
int插入新文章时要使用的文章ID。
如果指定,则不能与任何现有的文章ID冲突。默认值为0。post_category
int[]类别ID数组。
默认为'default_category'
选项的值。tags_input
array标签名、slug或ID的数组。默认为空。tax_input
array分类法的分类项(term)数组,分类名作为键名。
如果分类法是分层的,则分类项列表需要是term ID的数组或逗号分隔的ID字符串。
如果分类法是非分层的,则分类项列表可以是包含分类项名称或slug的数组,也可以是逗号分隔的名称或slug字符串。这是因为,在分级分类法中,子分类项可以与不同的父分类项具有相同的名称,因此连接它们的唯一方法是使用ID。默认值为空。meta_input
array文章meta值数组,键名是文章meta键。默认为空。page_template
string要使用的页面模板。
默认:
array()
$wp_error
bool可选-
失败时是否返回WP_Error。
默认:
false
$fire_after_hooks
bool可选-
是否触发insert后的钩子。
默认:
true
返回
int|WP_Error 成功时的文章ID。失败时的值0或WP_Error。
源码
更新日志
版本 | 描述 |
---|---|
5.6.0 | 添加了$fire_after_hooks 参数。 |
3.5.0 | 添加了$wp_error 参数,以允许在失败时返回WP_Error。 |
1.0.0 | 开始引入 |
使用示例
您还可以同时更新meta(如果需要)
$data = array( 'ID' => $post_id, 'post_content' => $content, 'meta_input' => array( 'meta_key' => $meta_value, 'another_meta_key' => $another_meta_value ) ); wp_update_post( $data );
示例
在调用wp_update_post()之前,需要创建一个数组来传递必要的元素。与wp_insert_post()不同,只需要传递要更新的文章的ID和要更新的元素。元素的名称应与数据库中的名称相匹配。
// Update post 37 $my_post = array( 'ID' => 37, 'post_title' => 'This is the post title.', 'post_content' => 'This is the updated content.', ); // Update the post into the database wp_update_post( $my_post ); Processing $wp_error
如果您的更新不起作用,可能会出现错误。最好将$wp_error设置为true,然后立即显示错误。
<?php // Of course, this should be done in an development environment only and commented out or removed after deploying to your production site. wp_update_post( $current_item, true ); if (is_wp_error($post_id)) { $errors = $post_id->get_error_messages(); foreach ($errors as $error) { echo $error; } } ?>
类别
类别需要作为与数据库中的类别ID匹配的整数数组传递。即使只有一个类别分配给该文章,情况也是如此。警告–无限循环
当由连接到save_post的操作(例如自定义metabox)执行时,wp_update_post()有可能创建无限循环。发生这种情况是因为(1)wp_update_post()导致save_post被触发,(2)当启用修订时(首先在创建修订时,然后在更新原始文章导致创建无休止的修订时),save_post被调用两次。如果您必须从save_post调用的代码更新文章,请确保post_type未设置为“revision”,并且$post对象确实需要更新。
同样,如果挂接到edit_attachment的操作包含对wp_update_post的函数调用,并传递一个键值为“ID”的数组参数和对应于附件的关联值,则该操作可能会导致无限循环。
请注意,您需要删除并添加从API/Action引用修改的钩子代码示例:save_post
<?php function my_function( $post_id ){ if ( ! wp_is_post_revision( $post_id ) ){ // unhook this function so it doesn't loop infinitely remove_action('save_post', 'my_function'); // update the post, which calls save_post again wp_update_post( $my_args ); // re-hook this function add_action('save_post', 'my_function'); } } add_action('save_post', 'my_function'); ?>
程序性地将“发布”(或“草稿”)更改为“未来”,并将文章安排在明天发布:
$time = strtotime( 'tomorrow' ); $my_post = array( 'ID' => 1, 'post_status' => 'future', 'post_date' => date( 'Y-m-d H:i:s', $time ), 'post_date_gmt' => gmdate( 'Y-m-d H:i:s', $time ), ); wp_update_post( $my_post );
如果丢失
post_content
字段中的backslashes,请使用wp_slash
函数准备数据:wp_update_post( wp_slash( array( 'ID' => $postId, 'post_content' => $newContent, ) ) );
设置
post_date
时,确保同时设置post_date_gmt
。// Change the post date on a post with a status other than 'draft', 'pending' or 'auto-draft' $arg = array( 'ID' => $post_id, 'post_date' => $post_date, 'post_date_gmt' => get_gmt_from_date( $post_date ), ); wp_update_post( $arg );
当您在“草稿”、“待定”或“自动草稿”发布状态上设置发布日期时,还应将
edit_date
设置为true,否则发布日期不会更改。// Change the post date on a post with a status 'draft', 'pending' or 'auto-draft' $arg = array( 'ID' => $post_id, 'post_date' => $post_date, 'post_date_gmt' => get_gmt_from_date( $post_date ), 'edit_date' => true, ); wp_update_post( $arg );
如果您使用此函数导入并应用一些更新,则它无法识别‘import_id’,因此请记住也使用“ID”。
无论如何,如果您使用wp_insert_post“导入”,然后使用wp_update_post,您将丢失所有特色图像,并且您在import/update之间添加的任何其他类别都将丢失。(插件“来自URL的特色图像”解决了特色图像问题,如果它被激活的话。)
此外,一些文档建议您可以使用作者的名称或类别的名称,但据我所知,当importing/updating内容时,您只能使用作者和类别的id号。
以编程方式发布文章,
// Puslish post 37 $my_post = array( 'ID' => 37, 'post_status' => 'publish', ); // Update the post into the database wp_update_post( $my_post );