WP_Term_Query::format_terms()protectedWP 6.0.0

Format response depending on field requested.

Method of the class: WP_Term_Query{}

No Hooks.

Return

WP_Term[]|Int[]|String[]. Array of terms / strings / ints depending on field requested.

Usage

// protected - for code of main (parent) or child class
$result = $this->format_terms( $term_objects, $_fields );
$term_objects(WP_Term[]) (required)
Array of term objects.
$_fields(string) (required)
Field to format.

Changelog

Since 6.0.0 Introduced.

WP_Term_Query::format_terms() code WP 6.6

protected function format_terms( $term_objects, $_fields ) {
	$_terms = array();
	if ( 'id=>parent' === $_fields ) {
		foreach ( $term_objects as $term ) {
			$_terms[ $term->term_id ] = $term->parent;
		}
	} elseif ( 'ids' === $_fields ) {
		foreach ( $term_objects as $term ) {
			$_terms[] = (int) $term->term_id;
		}
	} elseif ( 'tt_ids' === $_fields ) {
		foreach ( $term_objects as $term ) {
			$_terms[] = (int) $term->term_taxonomy_id;
		}
	} elseif ( 'names' === $_fields ) {
		foreach ( $term_objects as $term ) {
			$_terms[] = $term->name;
		}
	} elseif ( 'slugs' === $_fields ) {
		foreach ( $term_objects as $term ) {
			$_terms[] = $term->slug;
		}
	} elseif ( 'id=>name' === $_fields ) {
		foreach ( $term_objects as $term ) {
			$_terms[ $term->term_id ] = $term->name;
		}
	} elseif ( 'id=>slug' === $_fields ) {
		foreach ( $term_objects as $term ) {
			$_terms[ $term->term_id ] = $term->slug;
		}
	} elseif ( 'all' === $_fields || 'all_with_object_id' === $_fields ) {
		$_terms = $term_objects;
	}

	return $_terms;
}