WordPress.org

Ready to get started?Download WordPress

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.

order 
(string)
  • ASC - Default
  • DESC
orderby 
(string)
  • name - Default
  • count
  • slug
  • term_group
  • term_order
  • term_id
  • none
fields 
(string)
  • all - Default : all matching terms objects will be returned.
  • ids : terms ids will be returned
  • names : terms names will be returned
  • slugs : terms slugs will be returned
  • all_with_object_id : all matching terms objects will be returned.
  • tt_ids : terms taxonomies 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 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.