检索给定分类法或分类法列表中的术语。

原型

get_terms( string|array $args = array(), array $deprecated = '' )

描述

你可以在发送查询之前完全注入任何自定义项,并使用过滤器控制输出。

参数

$args (string|array) (Optional)  数组或参数字符串。有关已接受参数的信息,请参阅WP_Term_Query :: __ construct()。

$deprecated (array) (Optional) 参数数组,使用遗留函数参数格式时。如果存在,则此参数将被解释为$ args,并且第一个函数参数将被解析为分类法或分类法数组。

返回值

(array|int|WP_Error)  清单

源文件

路径:wp-includes/taxonomy.php

<?php
...
function get_terms( $args = array(), $deprecated = '' ) {
	$term_query = new WP_Term_Query();
	$defaults = array(
		'suppress_filter' => false,
	);
	/*
	 * Legacy argument format ($taxonomy, $args) takes precedence.
	 *
	 * We detect legacy argument format by checking if
	 * (a) a second non-empty parameter is passed, or
	 * (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies)
	 */
	$_args = wp_parse_args( $args );
	$key_intersect  = array_intersect_key( $term_query->query_var_defaults, (array) $_args );
	$do_legacy_args = $deprecated || empty( $key_intersect );
	if ( $do_legacy_args ) {
		$taxonomies = (array) $args;
		$args = wp_parse_args( $deprecated, $defaults );
		$args['taxonomy'] = $taxonomies;
	} else {
		$args = wp_parse_args( $args, $defaults );
		if ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) {
			$args['taxonomy'] = (array) $args['taxonomy'];
		}
	}
	if ( ! empty( $args['taxonomy'] ) ) {
		foreach ( $args['taxonomy'] as $taxonomy ) {
			if ( ! taxonomy_exists( $taxonomy ) ) {
				return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
			}
		}
	}
	// Don't pass suppress_filter to WP_Term_Query.
	$suppress_filter = $args['suppress_filter'];
	unset( $args['suppress_filter'] );
	$terms = $term_query->query( $args );
	// Count queries are not filtered, for legacy reasons.
	if ( ! is_array( $terms ) ) {
		return $terms;
	}
	if ( $suppress_filter ) {
		return $terms;
	}
	/**
	 * Filters the found terms.
	 *
	 * @since 2.3.0
	 * @since 4.6.0 Added the `$term_query` parameter.
	 *
	 * @param array         $terms      Array of found terms.
	 * @param array         $taxonomies An array of taxonomies.
	 * @param array         $args       An array of get_terms() arguments.
	 * @param WP_Term_Query $term_query The WP_Term_Query object.
	 */
	return apply_filters( 'get_terms', $terms, $term_query->query_vars['taxonomy'], $term_query->query_vars, $term_query );
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/get_terms/