Codex

Function Reference/wp get object terms

Contents

Description

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


Usage

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

Parameters

$object_ids
(string|array) (required) The id's of objects to retrieve terms from.
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 the $args parameter and for what can be contained in the string or array of that parameter, if it exists.

The first argument is called 'orderby' and has the default value of 'name'. Other supported values include 'count', 'slug', 'term_group', 'term_order', and 'term_id'.

The second argument is called 'order' and has the default value of 'ASC'. The only other value that will be acceptable is 'DESC'.

The final argument supported is called 'fields' and has the default value of 'all'. There are multiple other options that can be used instead. Supported values are as follows: 'all', 'ids', 'names', 'slugs', and finally 'all_with_object_id'.

The fields argument also decides what will be returned. If 'all' or 'all_with_object_id' is choosen or the default kept intact, then all matching terms objects will be returned. If 'ids', 'slugs' or 'names' is used, then an array of all matching term ids or term names will be returned respectively.

Return Values

(array|WP_Error) 
The requested term data 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 which 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').'">'.$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.