WP_REST_Terms_Controller::get_term() WordPress Method

The WP_REST_Terms_Controller::get_term() method is used to get a single term from a taxonomy. The method accepts two parameters: the taxonomy slug and the term slug. The method returns a WP_REST_Response object.

WP_REST_Terms_Controller::get_term( int $id ) #

Get the term, if the ID is valid.


Parameters

$id

(int)(Required)Supplied ID.


Top ↑

Return

(WP_Term|WP_Error) Term object if ID is valid, WP_Error otherwise.


Top ↑

Source

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

	protected function get_term( $id ) {
		$error = new WP_Error(
			'rest_term_invalid',
			__( 'Term does not exist.' ),
			array( 'status' => 404 )
		);

		if ( ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
			return $error;
		}

		if ( (int) $id <= 0 ) {
			return $error;
		}

		$term = get_term( (int) $id, $this->taxonomy );
		if ( empty( $term ) || $term->taxonomy !== $this->taxonomy ) {
			return $error;
		}

		return $term;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.2Introduced.

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.