WP_REST_Posts_Controller::handle_terms() WordPress Method

The WP_REST_Posts_Controller::handle_terms() method is used to handle terms for a post. This is a private method.

WP_REST_Posts_Controller::handle_terms( int $post_id, WP_REST_Request $request ) #

Updates the post’s terms from a REST request.


Parameters

$post_id

(int)(Required)The post ID to update the terms form.

$request

(WP_REST_Request)(Required)The request object with post and terms data.


Top ↑

Return

(null|WP_Error) WP_Error on an error assigning any of the terms, otherwise null.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

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


Top ↑

Changelog

Changelog
VersionDescription
4.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.