WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp get object terms

Description

Retrieves the terms associated with the given object(s) in the supplied taxonomies.

It should be noted that the results from wp_get_object_terms are not cached which will result in a db call everytime this function is called. For performance, functions like get_the_terms() (which the results of has been cached), should be used.

Usage

<?php wp_get_object_terms$object_ids$taxonomies$args ); ?>

Parameters

$object_ids
(string|array) (required) The id's of objects to retrieve terms for.
Default: None
$taxonomies
(string|array) (required) The taxonomies to retrieve terms from. For example: 'category', 'post_tag', 'taxonomy slug'
Default: None
$args
(array|string) (optional) Change what is returned
Default: array

Default Arguments

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

Argument Options

The following information has to do with the $args parameter and for what can be contained in the string or array of that parameter, if it exists.

order 
(string)
  • ASC - Default
  • DESC
orderby 
(string)
  • name - Default
  • count
  • slug
  • term_group
  • term_order
  • term_id
  • none
fields 
(string)
  • all - Default : all matching term's objects will be returned
  • ids : term's ids will be returned
  • names : term's names will be returned
  • slugs : term's slugs will be returned
  • all_with_object_id : all matching term's objects will be returned
  • tt_ids : term's taxonomy's ids will be returned

NOTE: Arguments are passed in the format used by wp_parse_args(). e.g.

Return Values

(array|WP_Error) 
Array of requested term objects, or empty array, if no terms found. WP_Error if $taxonomy does not exist. See is_wp_error() for more information.

Examples

Return a list of all 'product' taxonomy terms that are applied to $post:

$product_terms = wp_get_object_terms( $post->ID,  'product' );
if ( ! empty( $product_terms ) ) {
	if ( ! is_wp_error( $product_terms ) ) {
		echo '<ul>';
			foreach( $product_terms as $term ) {
				echo '<li><a href="' . get_term_link( $term->slug, 'product' ) . '">' . esc_html( $term->name ) . '</a></li>'; 
			}
		echo '</ul>';
	}
}

Notes

Change Log

Since: 2.3.0

Source File

wp_get_object_terms() is located in wp-includes/taxonomy.php.

Related

See also index of Function Reference and index of Template Tags.