wc_get_object_terms( int $object_id, string $taxonomy, string $field = null, string $index_key = null )

Helper to get cached object terms and filter by field using wp_list_pluck().


Description Description

Works as a cached alternative for wp_get_post_terms() and wp_get_object_terms().


Parameters Parameters

$object_id

(Required) Object ID.

$taxonomy

(Required) Taxonomy slug.

$field

(Optional) Field name.

Default value: null

$index_key

(Optional) Index key name.

Default value: null


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/wc-term-functions.php

function wc_get_object_terms( $object_id, $taxonomy, $field = null, $index_key = null ) {
	// Test if terms exists. get_the_terms() return false when it finds no terms.
	$terms = get_the_terms( $object_id, $taxonomy );

	if ( ! $terms || is_wp_error( $terms ) ) {
		return array();
	}

	return is_null( $field ) ? $terms : wp_list_pluck( $terms, $field, $index_key );
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.