get_term()

最后更新于:2021-11-27 00:54:34

get_term( int|GC_Term|object$term, string$taxonomy=”, string$output=OBJECT, string$filter=’raw’)

Get all Term data from database by Term ID.

参数

$term

(int|GC_Term|object) (Required) If integer, term data will be fetched from the database, or from the cache if available. If stdClass object (as in the results of a database query), will apply filters and return a GC_Term object with the $term data. If GC_Term, will return $term.

$taxonomy

(string) (Optional) Taxonomy name that $term is part of.

Default value: ”

$output

(string) (Optional) The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a GC_Term object, an associative array, or a numeric array, respectively.

Default value: OBJECT

$filter

(string) (Optional) How to sanitize term fields.

Default value: ‘raw’

响应

(GC_Term|array|GC_Error|null) GC_Term instance (or array) on success, depending on the $output value. GC_Error if $taxonomy does not exist. Null for miscellaneous failure.

源文件

文件: gc-includes/taxonomy.php

function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
	if ( empty( $term ) ) {
		return new GC_Error( 'invalid_term', __( 'Empty Term.' ) );
	}

	if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) {
		return new GC_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
	}

	if ( $term instanceof GC_Term ) {
		$_term = $term;
	} elseif ( is_object( $term ) ) {
		if ( empty( $term->filter ) || 'raw' === $term->filter ) {
			$_term = sanitize_term( $term, $taxonomy, 'raw' );
			$_term = new GC_Term( $_term );
		} else {
			$_term = GC_Term::get_instance( $term->term_id );
		}
	} else {
		$_term = GC_Term::get_instance( $term, $taxonomy );
	}

	if ( is_gc_error( $_term ) ) {
		return $_term;
	} elseif ( ! $_term ) {
		return null;
	}

	// Ensure for filters that this is not empty.
	$taxonomy = $_term->taxonomy;

	/**
	 * Filters a taxonomy term object.
	 *
	 * The {@see 'get_$taxonomy'} hook is also available for targeting a specific
	 * taxonomy.
	 *
	 * @since 2.3.0
	 * @since 4.4.0 `$_term` is now a `GC_Term` object.
	 *
	 * @param GC_Term $_term    Term object.
	 * @param string  $taxonomy The taxonomy slug.
	 */
	$_term = apply_filters( 'get_term', $_term, $taxonomy );

	/**
	 * Filters a taxonomy term object.
	 *
	 * The dynamic portion of the filter name, `$taxonomy`, refers
	 * to the slug of the term's taxonomy.
	 *
	 * @since 2.3.0
	 * @since 4.4.0 `$_term` is now a `GC_Term` object.
	 *
	 * @param GC_Term $_term    Term object.
	 * @param string  $taxonomy The taxonomy slug.
	 */
	$_term = apply_filters( "get_{$taxonomy}", $_term, $taxonomy );

	// Bail if a filter callback has changed the type of the `$_term` object.
	if ( ! ( $_term instanceof GC_Term ) ) {
		return $_term;
	}

	// Sanitize term, according to the specified filter.
	$_term->filter( $filter );

	if ( ARRAY_A === $output ) {
		return $_term->to_array();
	} elseif ( ARRAY_N === $output ) {
		return array_values( $_term->to_array() );
	}

	return $_term;
}