Retrieves the terms in a given taxonomy or list of taxonomies.
Description
You can fully inject any customizations to the query before it is sent, as well as control the output with a filter.
The return type varies depending on the value passed to $args['fields']
. See WP_Term_Query::get_terms() for details. In all cases, a WP_Error
object will be returned if an invalid taxonomy is requested.
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.
The ‘get_terms_orderby’ filter passes the ORDER BY
clause for the query along with the $args array.
Taxonomy or an array of taxonomies should be passed via the ‘taxonomy’ argument in the $args
array:
$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );
Prior to 4.5.0, taxonomy was passed as the first parameter of get_terms()
.
Parameters
$args
array|stringoptional- Array or string of arguments. See WP_Term_Query::__construct() for information on accepted arguments.
Default:
array()
$deprecated
array|stringoptional- Argument array, when using the legacy function parameter format.
If present, this parameter will be interpreted as$args
, and the first function parameter will be parsed as a taxonomy or array of taxonomies.
Default:
''
Source
function get_terms( $args = array(), $deprecated = '' ) {
$term_query = new WP_Term_Query();
$defaults = array(
'suppress_filter' => false,
);
/*
* Legacy argument format ($taxonomy, $args) takes precedence.
*
* We detect legacy argument format by checking if
* (a) a second non-empty parameter is passed, or
* (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies)
*/
$_args = wp_parse_args( $args );
$key_intersect = array_intersect_key( $term_query->query_var_defaults, (array) $_args );
$do_legacy_args = $deprecated || empty( $key_intersect );
if ( $do_legacy_args ) {
$taxonomies = (array) $args;
$args = wp_parse_args( $deprecated, $defaults );
$args['taxonomy'] = $taxonomies;
} else {
$args = wp_parse_args( $args, $defaults );
if ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) {
$args['taxonomy'] = (array) $args['taxonomy'];
}
}
if ( ! empty( $args['taxonomy'] ) ) {
foreach ( $args['taxonomy'] as $taxonomy ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
}
}
}
// Don't pass suppress_filter to WP_Term_Query.
$suppress_filter = $args['suppress_filter'];
unset( $args['suppress_filter'] );
$terms = $term_query->query( $args );
// Count queries are not filtered, for legacy reasons.
if ( ! is_array( $terms ) ) {
return $terms;
}
if ( $suppress_filter ) {
return $terms;
}
/**
* Filters the found terms.
*
* @since 2.3.0
* @since 4.6.0 Added the `$term_query` parameter.
*
* @param array $terms Array of found terms.
* @param array|null $taxonomies An array of taxonomies if known.
* @param array $args An array of get_terms() arguments.
* @param WP_Term_Query $term_query The WP_Term_Query object.
*/
return apply_filters( 'get_terms', $terms, $term_query->query_vars['taxonomy'], $term_query->query_vars, $term_query );
}
Hooks
Changelog
Version | Description |
---|---|
4.8.0 | Introduced 'suppress_filter' parameter. |
4.5.0 | Changed the function signature so that the $args array can be provided as the first parameter.Introduced 'meta_key' and 'meta_value' parameters. Introduced the ability to order results by metadata. |
4.4.0 | Introduced the ability to pass 'term_id' as an alias of 'id' for the orderby parameter.Introduced the 'meta_query' and 'update_term_meta_cache' parameters. Converted to return a list of WP_Term objects. |
4.2.0 | Introduced 'name' and 'childless' parameters. |
2.3.0 | Introduced. |
All get_terms attributes with default values:
(I try default from this article and it didn’t work. This works.)
As of WordPress 4.6.1, here’s the array that’s returned (to add to Leo’s helpful note above)
If parent => 0 is passed, only top-level terms will be returned
Get categories and subcategories by custom taxonomies:
Get all post categories ordered by count.
String syntax:
Array syntax:
Get all the links categories:
List all the terms in a custom taxonomy, without a link:
List all the terms, with link to term archive, separated by an interpunct (·):
Working with Advanced Custom Fields (field type: Taxonomy, output: Object) for filtering taxonomies.
Case: Create a filter with main categories defined by ACF field, and all another should belongs to *others*.
Solution:
Get all post categories ordered by count.
String syntax:
Array syntax:
List terms limit to those matching a specific metadata key and metadata value
Meta Query using a custom field, and then ordering using a different custom field.
Get list of terms that match a certain meta_key
If `get_terms` doesnt works for some odd reason with custom taxonomy not showing registered, try using `WP_Term_Query`:
Get all args from here: https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/
Custom Taxonomy and Sub Category Lists.
Your can use this to create list table also.
If you want to `get_terms() ` by `post_type`, here’s a way to do it:
Order by parent term ID – not documented, but also works.
Get All Terms id and name in a array for make dropdown select:
list categories( or custom taxonomy ) and subcategory in organazid way
If you have a term assigned to something else than a post, here is a warning:
'hide_empty'
is default true so it only shows terms assigned to posts. So if you get an empty Array returned be aware that terms (e.g. categories) assigned to attachments for example are not shown by default unless you use'hide_empty' => false
:Be very careful as the code below:
can return valid terms, but can also return a
WP_Error
if, for example, the taxonomy is invalid…Therefore this check alone isn’t enough to save you from bugs in your code:
Instead, better to use the one below:
Categories and Tags are the two pre-defined Taxonomies. The Taxonomy Name, when included as an element ‘taxonomy’ of the array specified as the first parameter $args, has a value of ‘category’ for Categories and ‘post_tag’ for Tags.
'taxonomy' => 'category'
'taxonomy' => 'post_tag'
As per https://core.trac.wordpress.org/ticket/41813 if you use this to in combination with a custom taxonomy which is used for media/attachments. You may find that all works well except the count of the terms when using get_terms ( which returns nothing ).
To fix this you may need to use this call in register_taxonomy
'update_count_callback' => '_update_generic_term_count',
( https://core.trac.wordpress.org/ticket/41813#comment:8 )
Loop through taxonomy and list all subcategories.
Get all child taxonomy of a parent taxonomy
Get Terms filtered by first letter
Get terms ordered by value of meta-key: