WordPress, a widely used content management system, offers a wide range of features that facilitate data manipulation and retrieval for developers. Among these features, get_terms() stands out as a highly effective function for retrieving taxonomy terms in WordPress. This article aims to thoroughly examine the capabilities of get_terms(), including its syntax, parameters, and practical uses.
The get_terms() Function
Let’s take a look at the get_terms() function:
<?php
get_terms(array|string $args = [], array|string $deprecated = '');
Here, $args
is an array or string of arguments that customize the query, and $deprecated
is an optional parameter that is no longer recommended for use. Let’s explore the essential parameters within the $args
array:
- $taxonomy (string|string[]): Taxonomy name, or array of taxonomy names, to which results should be limited.
- $object_ids (int|int[]): Object ID, or array of object IDs. Results will be limited to terms associated with these objects.
- $orderby (string): Field(s) to order terms by. Accepts various options such as ‘name’, ‘slug’, ‘term_group’, ‘term_id’, ‘id’, ‘description’, ‘parent’, ‘term_order’, ‘count’, ‘include’, ‘slug__in’, ‘meta_value’, ‘meta_value_num’, the value of
$meta_key
, the array keys of$meta_query
, and ‘none’. Default ‘name’. - $order (string): Whether to order terms in ascending or descending order. Accepts ‘ASC’ (ascending) or ‘DESC’ (descending). Default ‘ASC’.
- $hide_empty (bool|int): Whether to hide terms not assigned to any posts. Accepts 1|true or 0|false. Default 1|true.
- $include (int[]|string): Array or comma/space-separated string of term IDs to include. Default empty array.
- $exclude (int[]|string): Array or comma/space-separated string of term IDs to exclude. If
$include
is non-empty,$exclude
is ignored. Default empty array. - $exclude_tree (int[]|string): Array or comma/space-separated string of term IDs to exclude along with all of their descendant terms. If
$include
is non-empty,$exclude_tree
is ignored. Default empty array. - $number (int|string): Maximum number of terms to return. Accepts ”|0 (all) or any positive number. Default ”|0 (all).
- $offset (int): The number by which to offset the terms query. Default empty.
- $fields (string): Term fields to query for. Accepts options like ‘all’, ‘all_with_object_id’, ‘ids’, ‘tt_ids’, ‘names’, ‘slugs’, ‘count’, ‘id=>parent’, ‘id=>name’, ‘id=>slug’. Default ‘all’.
- $count (bool): Whether to return a term count. If true, will take precedence over
$fields
. Default false. - $name (string|string[]): Name or array of names to return term(s) for. Default empty.
- $slug (string|string[]): Slug or array of slugs to return term(s) for. Default empty.
- $term_taxonomy_id (int|int[]): Term taxonomy ID, or array of term taxonomy IDs, to match when querying terms.
- $hierarchical (bool): Whether to include terms that have non-empty descendants (even if
$hide_empty
is set to true). Default true. - $search (string): Search criteria to match terms. Will be SQL-formatted with wildcards before and after. Default empty.
- $name__like (string): Retrieve terms with criteria by which a term is LIKE
$name__like
. Default empty. - $description__like (string): Retrieve terms where the description is LIKE
$description__like
. Default empty. - $pad_counts (bool): Whether to pad the quantity of a term’s children in the quantity of each term’s “count” object variable. Default false.
- $get (string|string[]): Whether to return terms regardless of ancestry or whether the terms are empty. Accepts ‘all’ or ” (disabled). Default ”.
- $child_of (int): Term ID to retrieve child terms of. If multiple taxonomies are passed,
$child_of
is ignored. Default 0. - $parent (int): Parent term ID to retrieve direct-child terms of. Default empty.
- $childless (bool): True to limit results to terms that have no children. This parameter has no effect on non-hierarchical taxonomies. Default false.
- $cache_domain (string): Unique cache key to be produced when this query is stored in an object cache. Default ‘core’.
- $cache_results (bool): Whether to cache term information. Default true.
- $update_term_meta_cache (bool): Whether to prime meta caches for matched terms. Default true.
- $meta_key (string|string[]): Meta key or keys to filter by.
- $meta_value (string|string[]): Meta value or values to filter by.
- $meta_compare (string): MySQL operator used for comparing the meta value. See
WP_Meta_Query::__construct()
for accepted values and the default value. - $meta_compare_key (string): MySQL operator used for comparing the meta key. See
WP_Meta_Query::__construct()
for accepted values and the default value. - $meta_type (string): MySQL data type that the meta_value column will be CAST to for comparisons. See
WP_Meta_Query::__construct()
for accepted values and the default value. - $meta_type_key (string): MySQL data type that the meta_key column will be CAST to for comparisons. See
WP_Meta_Query::__construct()
for accepted values and the default value. - $meta_query (array): An associative array of
WP_Meta_Query
arguments. SeeWP_Meta_Query::__construct()
for accepted values.
Practical Applications
Now that we understand the syntax and parameters of get_terms()
, let’s explore some practical applications.
1. Displaying a List of Categories or Tags
<?php
$categories = get_terms( 'category' );
foreach ( $categories as $category ) {
echo '<a href="' . get_term_link( $category ) . '">' . $category->name . '</a>';
}
This example retrieves and displays a list of categories with links to their respective archive pages.
2. Custom Queries with Additional Parameters
<?php
$args = array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
'orderby' => 'count',
'order' => 'DESC',
);
$popular_tags = get_terms($args);
Here, we customize the query to retrieve popular tags by setting the orderby
parameter to ‘count’ and ordering them in descending order.
3. Excluding Specific Terms
<?php
$args = array(
'taxonomy' => 'category',
'exclude' => [1, 3, 5],
);
$categories = get_terms($args);
In this example, the function retrieves categories while excluding specific term IDs.
Conclusion
The get_terms() function within WordPress serves as a potent resource for developers seeking to access and manipulate taxonomy terms. By comprehending its syntax and parameters, one can customize queries to meet specific requirements, thereby making it an invaluable asset for constructing dynamic and personalized websites. Whether the objective is to create a bespoke navigation menu, showcase a tag cloud, or implement a distinctive taxonomy-related feature, get_terms() unlocks a plethora of possibilities in WordPress development.