On this page
WP_REST_Posts_Controller::handle_terms( int $post_id, WP_REST_Request $request ): null|WP_Error
Updates the post’s terms from a REST request.
Parameters
$post_idint Required-
The post ID to update the terms form.
$requestWP_REST_Request Required-
The request object with post and terms data.
Return
null|WP_Error WP_Error on an error assigning any of the terms, otherwise null.
Source
File: wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php. View all references
protected function handle_terms( $post_id, $request ) {
$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
foreach ( $taxonomies as $taxonomy ) {
$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
if ( ! isset( $request[ $base ] ) ) {
continue;
}
$result = wp_set_object_terms( $post_id, $request[ $base ], $taxonomy->name );
if ( is_wp_error( $result ) ) {
return $result;
}
}
}
Related
Uses
| Uses | Description |
|---|---|
| wp_list_filter() wp-includes/functions.php | Filters a list of objects, based on a set of key => value arguments. |
| wp_set_object_terms() wp-includes/taxonomy.php | Creates term and taxonomy relationships. |
| get_object_taxonomies() wp-includes/taxonomy.php | Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Posts_Controller::create_item() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Creates a single post. |
| WP_REST_Posts_Controller::update_item() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Updates a single post. |
Changelog
| Version | Description |
|---|---|
| 4.7.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/handle_terms