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

es:Function Reference/get terms


Recupera los términos de una taxonomía o lista de taxonomías.

Devuelve un array object con los términos o un WP_error si una de las taxonomías no existe.


<?php get_terms$taxonomia$args ); ?>

Uso por defecto

// Listar las taxonomías 'post_tag' y 'my_tax'
$taxonomia = array( 

$args = array(
    'orderby'           => 'name', 
    'order'             => 'ASC',
    'hide_empty'        => true, 
    'exclude'           => array(), 
    'exclude_tree'      => array(), 
    'include'           => array(),
    'number'            => '', 
    'fields'            => 'all', 
    'slug'              => '',
    'parent'            => '',
    'hierarchical'      => true, 
    'child_of'          => 0,
    'childless'         => false,
    'get'               => '', 
    'name__like'        => '',
    'description__like' => '',
    'pad_counts'        => false, 
    'offset'            => '', 
    'search'            => '', 
    'cache_domain'      => 'core'

$terms = get_terms($taxonomies, $args);


(string|array) (required) Las taxonomías que se van a recuperar. (Default Taxonomies)
Default: None
(string|array) (optional) Cambia lo que se devuelve.
Default: array

Posibles argumentos

  • id
  • count
  • name - Defecto
  • slug
  • term_group - No completamente implementado (avoid using)
  • none
  • ASC - Defecto
  • DESC
(boolean) Whether to not return empty $terms.
  • 1 (true) - Defecto (i.e. Do not show empty terms)
  • 0 (false)
(integer|string|array) Un array (arreglo) de ids de term a excluir. También acepta una cadena de ids separada por comas.
(integer|string|array) Un array (arreglo) de ids de term padres a excluir.
(integer|string|array) An array of term ids to include. Empty returns all.
(integer) El máximo número de terms a retornar. Por defecto nos retorna todos ellos.
  • all - returns an array of term objects - Default
  • ids - returns an array of integers
  • names - returns an array of strings
  • count - (3.2+) returns the number of terms found
  • id=>parent - returns an associative array where the key is the term id and the value is the parent term id if present or 0
  • id=>slug - returns an associative array where the key is the term id and the value is the slug
  • id=>name - returns an associative array where the key is the term id and the value is the name
(string|array) Returns terms whose "slug" matches this value. Default is empty string.
(integer) Get direct children of this term (only terms whose explicit parent is this value). If 0 is passed, only top-level terms are returned. Default is an empty string.
(boolean) Whether to include terms that have non-empty descendants (even if 'hide_empty' is set to true).
  • 1 (true) - Default
  • 0 (false)
(integer) Get all descendents of this term. Default is 0. Note: the difference between child_of and parent is that where parent only gets direct children of the parent term (ie: 1 level down), child_of gets all descendants (as many levels as are available)
(boolean) Returns terms that have no children if taxonomy is hierarchical, all terms if taxonomy is not hierarchical
(string) Default is nothing . Allow for overwriting 'hide_empty' and 'child_of', which can be done by setting the value to 'all'.
(string) The term name you wish to match. It does a LIKE '%string%' query against the term name only. This matches terms that contain the 'name__like' string. Note: This was changed in WordPress 3.7, when previously name__like matched terms that begin with the string. See ticket #8214.
(string) Returned terms' descriptions will contain the value of 'description__like', case-insensitive. Default is empty string. It does a LIKE '%string%' query against the term description only. This matches terms that contain the 'description__like' string.
(boolean) If true, count all of the children along with the $terms.
  • 1 (true)
  • 0 (false) - Default
(integer) The number by which to offset the terms query. Must be used in conjunction with 'number' otherwise 'offset' is ignored and the entire result set is returned.
(string) The term name you wish to match. It does a LIKE '%search_string%' query against the term name and slug. This matches terms that contain the 'search_string' string.
(string) Version 3.2 and above. The 'cache_domain' argument enables a unique cache key to be produced when the query produced by get_terms() is stored in object cache. For instance, if you are using one of this function's filters to modify the query (such as 'terms_clauses'), setting 'cache_domain' to a unique value will not overwrite the cache for similar queries. Default value is 'core'.

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

Valores devueltos

Array of term objects or an empty array if no terms were found. WP_Error if any of $taxonomies does not exist. If the 'fields' argument was 'count', the number of terms found will be returned as a string.

The fields returned are:

  • term_id
  • name
  • slug
  • term_group
  • term_taxonomy_id
  • taxonomy
  • description
  • parent
  • count

Warning: string vs integer confusion! Field values, including term_id are returned in string format. Before further use, typecast numeric values to actual integers, otherwise WordPress will mix up term_ids and slugs which happen to have only numeric characters


Get all post categories ordered by count.

String syntax:

$categories = get_terms( 'category', 'orderby=count&hide_empty=0' );

Array syntax:

 $categories = get_terms( 'category', array(
 	'orderby'    => 'count',
 	'hide_empty' => 0,
 ) );

Get all the links categories:

$mylinks_categories = get_terms( 'link_category', 'orderby=count&hide_empty=0' );

List all the terms in a custom taxonomy, without a link:

 $terms = get_terms( 'my_taxonomy' );
 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
     echo '<ul>';
     foreach ( $terms as $term ) {
       echo '<li>' . $term->name . '</li>';
     echo '</ul>';

List all the terms, with link to term archive, separated by an interpunct (·):

$args = array( 'hide_empty' => 0 );

$terms = get_terms( 'my_term', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
    $count = count( $terms );
    $i = 0;
    $term_list = '<p class="my_term-archive">';
    foreach ( $terms as $term ) {
    	$term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>';
    	if ( $count != $i ) {
            $term_list .= ' &middot; ';
        else {
            $term_list .= '</p>';
    echo $term_list;


You can inject any customizations to the query before it is sent or control the output with filters.

The 'get_terms' filter will be called when the cache has the term and will pass the found term along with the array of $taxonomies and array of $args.

This filter is also called before the array of terms is passed and will pass the array of terms, along with the $taxonomies and $args.

The 'list_terms_exclusions' filter passes the compiled exclusions along with the $args.

Log de cambios

Archivo fuente

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



Mira también el índice de referencia de funciones y el índice de etiquetas de plantilla.