WP_REST_Posts_Controller::handle_terms()protectedWP 4.7.0

Updates the post's terms from a REST request.

Метод класса: WP_REST_Posts_Controller{}

Хуков нет.

Возвращает

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

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->handle_terms( $post_id, $request );
$post_id(int) (обязательный)
The post ID to update the terms form.
$request(WP_REST_Request) (обязательный)
The request object with post and terms data.

Список изменений

С версии 4.7.0 Введена.

Код WP_REST_Posts_Controller::handle_terms() WP 6.5.2

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