acf_decode_taxonomy_term()ACF 5.0.0

acf_decode_taxonomy_term

This function will return the taxonomy and term slug for a given value

Хуков нет.

Возвращает

(Массив).

Использование

acf_decode_taxonomy_term( $value );
$value (обязательный)
-

Список изменений

С версии 5.0.0 Введена.

Код acf_decode_taxonomy_term() ACF 6.0.4

function acf_decode_taxonomy_term( $value ) {

	// vars
	$data = array(
		'taxonomy' => '',
		'term'     => '',
	);

	// int
	if ( is_numeric( $value ) ) {

		$data['term'] = $value;

		// string
	} elseif ( is_string( $value ) ) {

		$value            = explode( ':', $value );
		$data['taxonomy'] = isset( $value[0] ) ? $value[0] : '';
		$data['term']     = isset( $value[1] ) ? $value[1] : '';

		// error
	} else {

		return false;

	}

	// allow for term_id (Used by ACF v4)
	if ( is_numeric( $data['term'] ) ) {

		// global
		global $wpdb;

		// find taxonomy
		if ( ! $data['taxonomy'] ) {

			$data['taxonomy'] = $wpdb->get_var( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d LIMIT 1", $data['term'] ) );

		}

		// find term (may have numeric slug '123')
		$term = get_term_by( 'slug', $data['term'], $data['taxonomy'] );

		// attempt get term via ID (ACF4 uses ID)
		if ( ! $term ) {
			$term = get_term( $data['term'], $data['taxonomy'] );
		}

		// bail early if no term
		if ( ! $term ) {
			return false;
		}

		// update
		$data['taxonomy'] = $term->taxonomy;
		$data['term']     = $term->slug;

	}

	// return
	return $data;

}