WP_Term_Query::format_terms() WordPress Method

This method is used to format the terms for display. It is used internally by the WP_Term_Query::get_terms() method.

WP_Term_Query::format_terms( WP_Term[] $term_objects, string $_fields ) #

Format response depending on field requested.


Parameters

$term_objects

(WP_Term[])(Required)Array of term objects.

$_fields

(string)(Required)Field to format.


Top ↑

Return

(WP_Term[]|int[]|string[]) Array of terms / strings / ints depending on field requested.


Top ↑

Source

File: wp-includes/class-wp-term-query.php

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


Top ↑

Changelog

Changelog
VersionDescription
6.0.0Introduced.

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.