WP_REST_Posts_Controller::create_item(WP_REST_请求 $请求 ):WP_REST_响应|WP_错误

创建单个帖子。

参数

$请求WP_REST_请求必修的
有关请求的完整详细信息。

返回

WP_REST_响应|WP_错误成功时的响应对象,或WP_错误对象失败。

来源

公共函数create_item($request){if(!empty($request['id'])){返回新的WP_Error(“rest_post_exists”,__('无法创建现有帖子'),数组(“状态”=>400));}$prepared_post=$this->prepare_item_for_database($request);if(iswp_error($prepared_post)){返回$prepared_post;}$prepared_post->post_type=$this->post_type;if(!空($prepared_post->post_name)&& ! 空($prepared_post->post_status)&&in_array($prepared_post->post_status,数组('draft','pending'),true)) {/**`wp_unique_post_slug()`为'draft'或'pending'帖子返回相同的slug。**为了确保生成唯一的slug,请传递具有“发布”状态的post数据。*/$prepared_post->post_name=wp_unique_post_slug($prepared_post->post_name,$prepared_post->id,“发布”,$prepared_post->post_type,$prepared_post->帖子父);}$post_id=wp_insert_post(wp_slash((数组)$prepared_post),true,false);if(iswp_error($post_id)){if(“db_insert_error”===$post_id->get_error_code()){$post_id->add_data(数组(“状态”=>500));}其他{$post_id->add_data(数组(“状态”=>400));}返回$post_id;}$post=获取post($post_id);/***在通过RESTAPI创建或更新单个帖子后激发。**钩子名称的动态部分“$this->post_type”指的是post-type slug。**可能的挂钩名称包括:**-`rest_insert_post`*-`rest_insert_page`*-`rest_insert_attachment`**@自4.7.0起**@param WP_Post$Post插入或更新了Post对象。*@param WP_REST_Request$请求请求对象。*@param bool$创建帖子时为True,更新时为false。*/do_action(“rest_insert_{$this->post_type}”,$post,$request,true);$schema=$this->get_item_schema();if(!空($schema['properties']['sticky']){if(!空($request['sticky']){stick_post($post_id);}其他{unstick_post($post_id);}}if(!空($schema['properties']['featuredmedia'])&&isset($request['featedmedia'])){$this->handle_featured_media($request['feature_demedia'],$post_id);}if(!空($schema['properties']['format'])&&!空($request['format']){set_post_format($post,$request['format']);}if(!空($schema['properties']['template'])&&isset($request['temp']){$this->handle_template($request['template'],$post_id,true);}$terms_update=$this->handle_terms($post_id,$request);if(is_wp_error($terms_update)){返回$terms_update;}if(!空($schema['properties']['meta'])&&isset($request['meta']){$meta_update=$this->meta->update_value($request['meta'],$post_id);if(iswp_error($meta_update)){返回$meta_update;}}$post=获取post($post_id);$fields_update=$this->update_additional_fields_for_object($post,$request);if(iswp_error($fields_update)){返回$fields_update;}$request->set_param('context','edit');/***通过REST API完全创建或更新单个帖子后激发。**钩子名称的动态部分“$this->post_type”指的是post-type slug。**可能的挂钩名称包括:**-`rest_after_insert_post`*-`rest_after_insert_page`*-`rest_after_insert_attachment`**@自5.0.0起**@param WP_Post$Post插入或更新了Post对象。*@param WP_REST_Request$请求请求对象。*@param bool$创建帖子时为True,更新时为false。*/do_action(“rest_after_insert_{$this->post_type}”,$post,$request,true);wp_after_insert_post($post,false,null);$response=$this->prepare_item_for_response($post,$request);$response=rest_ensure_response($response);$response->set_status(201);$response->header(“位置”,rest_url(rest_get_route_for_post($post));返回$response;}

挂钩

do_操作(“rest_after_insert_{$this->post_type}”,WP_支柱 $帖子,WP_REST_请求 $请求,布尔 $正在创建 )

通过REST API完全创建或更新单个帖子后激发。

do_操作(“rest_insert_{$this->post_type}”,WP_支柱 $帖子,WP_REST_请求 $请求,布尔 $正在创建 )

在通过RESTAPI创建或更新单个帖子后激发。

变更日志

版本描述
4.7.0介绍。

用户贡献的笔记

你必须登录在能够发表注释或反馈之前。