wp_get_object_terms

Definition:
function wp_get_object_terms($object_ids, $taxonomies, $args = array() {}

Retrieves the terms associated with the given object(s), in the supplied taxonomies.
The following information has to do the $args parameter and for what can be contained in the string or array of that parameter, if it exists.

Parameters

  • int|array $object_ids: The ID(s) of the object(s) to retrieve.
  • string|array $taxonomies: The taxonomies to retrieve terms from.
  • array|string $args: Change what is returned

Return values

returns:The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist.

Defined filters

  • wp_get_object_terms
    apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args)

Source code

function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {

	global $wpdb;



	if ( !is_array($taxonomies) )

		$taxonomies = array($taxonomies);



	foreach ( (array) $taxonomies as $taxonomy ) {

		if ( ! taxonomy_exists($taxonomy) )

			return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));

	}



	if ( !is_array($object_ids) )

		$object_ids = array($object_ids);

	$object_ids = array_map('intval', $object_ids);



	$defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');

	$args = wp_parse_args( $args, $defaults );



	$terms = array();

	if ( count($taxonomies) > 1 ) {

		foreach ( $taxonomies as $index => $taxonomy ) {

			$t = get_taxonomy($taxonomy);

			if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {

				unset($taxonomies[$index]);

				$terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));

			}

		}

	} else {

		$t = get_taxonomy($taxonomies[0]);

		if ( isset($t->args) && is_array($t->args) )

			$args = array_merge($args, $t->args);

	}



	extract($args, EXTR_SKIP);



	if ( 'count' == $orderby )

		$orderby = 'tt.count';

	else if ( 'name' == $orderby )

		$orderby = 't.name';

	else if ( 'slug' == $orderby )

		$orderby = 't.slug';

	else if ( 'term_group' == $orderby )

		$orderby = 't.term_group';

	else if ( 'term_order' == $orderby )

		$orderby = 'tr.term_order';

	else if ( 'none' == $orderby ) {

		$orderby = '';

		$order = '';

	} else {

		$orderby = 't.term_id';

	}



	// tt_ids queries can only be none or tr.term_taxonomy_id

	if ( ('tt_ids' == $fields) && !empty($orderby) )

		$orderby = 'tr.term_taxonomy_id';



	if ( !empty($orderby) )

		$orderby = "ORDER BY $orderby";



	$taxonomies = "'" . implode("', '", $taxonomies) . "'";

	$object_ids = implode(', ', $object_ids);



	$select_this = '';

	if ( 'all' == $fields )

		$select_this = 't.*, tt.*';

	else if ( 'ids' == $fields )

		$select_this = 't.term_id';

	else if ( 'names' == $fields )

		$select_this = 't.name';

	else if ( 'slugs' == $fields )

		$select_this = 't.slug';

	else if ( 'all_with_object_id' == $fields )

		$select_this = 't.*, tt.*, tr.object_id';



	$query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order";



	if ( 'all' == $fields || 'all_with_object_id' == $fields ) {

		$terms = array_merge($terms, $wpdb->get_results($query));

		update_term_cache($terms);

	} else if ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) {

		$terms = array_merge($terms, $wpdb->get_col($query));

	} else if ( 'tt_ids' == $fields ) {

		$terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order");

	}



	if ( ! $terms )

		$terms = array();



	return apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args);

}

3723

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: