On this page
get_terms( array|string $args = array(), array|string $deprecated = '' ): WP_Term[]|int[]|string[]|string|WP_Error
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.
Prior to 4.5.0, the first parameter of get_terms() was a taxonomy or list of taxonomies:
$terms = get_terms( 'post_tag', array(
'hide_empty' => false,
) );
Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:
$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );
Parameters
$argsarray|string Optional-
Array or string of arguments. See WP_Term_Query::__construct() for information on accepted arguments.
More Arguments from WP_Term_Query::__construct( ... $query )
Array or query string of term query parameters.
taxonomystring|string[]Taxonomy name, or array of taxonomy names, to which results should be limited.object_idsint|int[]Object ID, or array of object IDs. Results will be limited to terms associated with these objects.orderbystringField(s) to order terms by. Accepts:
- Term fields (
'name','slug','term_group','term_id','id','description','parent','term_order'). Unless$object_idsis not empty,'term_order'is treated the same as'term_id'. 'count'to use the number of objects associated with the term.'include'to match the'order'of the$includeparam.'slug__in'to match the'order'of the$slugparam.'meta_value''meta_value_num'.- The value of
$meta_key. - The array keys of
$meta_query. 'none'to omit the ORDER BY clause.
'name'.- Term fields (
orderstringWhether to order terms in ascending or descending order.
Accepts'ASC'(ascending) or'DESC'(descending).
Default'ASC'.hide_emptybool|intWhether to hide terms not assigned to any posts. Accepts1|trueor0|false. Default1|true.includeint[]|stringArray or comma/space-separated string of term IDs to include.
Default empty array.excludeint[]|stringArray or comma/space-separated string of term IDs to exclude.
If$includeis non-empty,$excludeis ignored.
Default empty array.exclude_treeint[]|stringArray or comma/space-separated string of term IDs to exclude along with all of their descendant terms. If$includeis non-empty,$exclude_treeis ignored. Default empty array.numberint|stringMaximum number of terms to return. Accepts(all) or any positive number. Default''|0(all). Note that''|0$numbermay not return accurate results when coupled with$object_ids.
See #41796 for details.offsetintThe number by which to offset the terms query.fieldsstringTerm fields to query for. Accepts:
'all'Returns an array of complete term objects (WP_Term[]).'all_with_object_id'Returns an array of term objects with the'object_id'param (WP_Term[]). Works only when the$object_idsparameter is populated.'ids'Returns an array of term IDs (int[]).'tt_ids'Returns an array of term taxonomy IDs (int[]).'names'Returns an array of term names (string[]).'slugs'Returns an array of term slugs (string[]).'count'Returns the number of matching terms (int).'id=>parent'Returns an associative array of parent term IDs, keyed by term ID (int[]).'id=>name'Returns an associative array of term names, keyed by term ID (string[]).'id=>slug'Returns an associative array of term slugs, keyed by term ID (string[]).
'all'.countboolWhether to return a term count. If true, will take precedence over$fields. Default false.namestring|string[]Name or array of names to return term(s) for.
slugstring|string[]Slug or array of slugs to return term(s) for.
term_taxonomy_idint|int[]Term taxonomy ID, or array of term taxonomy IDs, to match when querying terms.hierarchicalboolWhether to include terms that have non-empty descendants (even if$hide_emptyis set to true). Default true.searchstringSearch criteria to match terms. Will be SQL-formatted with wildcards before and after.name__likestringRetrieve terms with criteria by which a term is LIKE$name__like.description__likestringRetrieve terms where the description is LIKE$description__like.pad_countsboolWhether to pad the quantity of a term's children in the quantity of each term's "count" object variable. Default false.getstringWhether to return terms regardless of ancestry or whether the terms are empty. Accepts'all'or''(disabled). Default''.child_ofintTerm ID to retrieve child terms of. If multiple taxonomies are passed,$child_ofis ignored. Default 0.parentintParent term ID to retrieve direct-child terms of.
childlessboolTrue to limit results to terms that have no children.
This parameter has no effect on non-hierarchical taxonomies.
Default false.cache_domainstringUnique cache key to be produced when this query is stored in an object cache. Default'core'.update_term_meta_cacheboolWhether to prime meta caches for matched terms. Default true.meta_keystring|string[]Meta key or keys to filter by.meta_valuestring|string[]Meta value or values to filter by.meta_comparestringMySQL operator used for comparing the meta value.
See WP_Meta_Query::__construct() for accepted values and default value.meta_compare_keystringMySQL operator used for comparing the meta key.
See WP_Meta_Query::__construct() for accepted values and default value.meta_typestringMySQL data type that the meta_value column will be CAST to for comparisons.
See WP_Meta_Query::__construct() for accepted values and default value.meta_type_keystringMySQL data type that the meta_key column will be CAST to for comparisons.
See WP_Meta_Query::__construct() for accepted values and default value.meta_queryarrayAn associative array of WP_Meta_Query arguments.
See WP_Meta_Query::__construct() for accepted values.
Default:
array() $deprecatedarray|string Optional-
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:
''
Return
WP_Term[]|int[]|string[]|string|WP_Error Array of terms, a count thereof as a numeric string, or WP_Error if any of the taxonomies do not exist.
See the function description for more information.
Source
File: wp-includes/taxonomy.php. View all references
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
Related
Uses
| Uses | Description |
|---|---|
| WP_Term_Query::__construct() wp-includes/class-wp-term-query.php | Constructor. |
| taxonomy_exists() wp-includes/taxonomy.php | Determines whether the taxonomy name exists. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| wp_parse_args() wp-includes/functions.php | Merges user defined arguments into defaults array. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| WP_Posts_List_Table::formats_dropdown() wp-admin/includes/class-wp-posts-list-table.php | Displays a formats drop-down for filtering items. |
| taxonomy_meta_box_sanitize_cb_input() wp-admin/includes/post.php | Sanitizes POST values from an input taxonomy metabox. |
| WP_REST_Terms_Controller::get_items() wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php | Retrieves terms associated with a taxonomy. |
| WP_Term_Query::get_terms() wp-includes/class-wp-term-query.php | Retrieves the query results. |
| WP_Customize_Nav_Menus::search_available_items_query() wp-includes/class-wp-customize-nav-menus.php | Performs post queries for available-item searching. |
| WP_Customize_Nav_Menus::load_available_items_query() wp-includes/class-wp-customize-nav-menus.php | Performs the post_type and taxonomy queries for loading available menu items. |
| export_wp() wp-admin/includes/export.php | Generates the WXR export file for download. |
| wp_terms_checklist() wp-admin/includes/template.php | Outputs an unordered list of checkbox input elements labelled with term names. |
| wp_popular_terms_checklist() wp-admin/includes/template.php | Retrieves a list of the most popular terms from the specified taxonomy. |
| wp_link_category_checklist() wp-admin/includes/template.php | Outputs a link category checklist element. |
| wp_ajax_get_tagcloud() wp-admin/includes/ajax-actions.php | Ajax handler for getting a tagcloud. |
| wp_ajax_ajax_tag_search() wp-admin/includes/ajax-actions.php | Ajax handler for tag search. |
| WP_Terms_List_Table::prepare_items() wp-admin/includes/class-wp-terms-list-table.php | |
| _wp_ajax_menu_quick_search() wp-admin/includes/nav-menu.php | Prints the appropriate response to a menu quick search. |
| wp_nav_menu_item_taxonomy_meta_box() wp-admin/includes/nav-menu.php | Displays a meta box for a taxonomy menu item. |
| Walker_Category::start_el() wp-includes/class-walker-category.php | Starts the element output. |
| wp_tag_cloud() wp-includes/category-template.php | Displays a tag cloud. |
| wp_dropdown_categories() wp-includes/category-template.php | Displays or retrieves the HTML dropdown list of categories. |
| get_tags() wp-includes/category.php | Retrieves all post tags. |
| get_categories() wp-includes/category.php | Retrieves a list of category objects. |
| get_category_by_path() wp-includes/category.php | Retrieves a category based on URL containing the category slug. |
| get_all_category_ids() wp-includes/deprecated.php | Retrieves all category IDs. |
| WP_Widget_Links::form() wp-includes/widgets/class-wp-widget-links.php | Outputs the settings form for the Links widget. |
| _get_term_hierarchy() wp-includes/taxonomy.php | Retrieves children of taxonomy as term IDs. |
| wp_get_object_terms() wp-includes/taxonomy.php | Retrieves the terms associated with the given object(s), in the supplied taxonomies. |
| wp_insert_term() wp-includes/taxonomy.php | Adds a new term to the database. |
| wp_count_terms() wp-includes/taxonomy.php | Counts how many terms are in taxonomy. |
| get_term_by() wp-includes/taxonomy.php | Gets all term data from database by term field and data. |
| term_exists() wp-includes/taxonomy.php | Determines whether a taxonomy term exists. |
| wp_list_bookmarks() wp-includes/bookmark-template.php | Retrieves or echoes all of the bookmarks. |
| wp_get_nav_menus() wp-includes/nav-menu.php | Returns all navigation menu objects. |
| wp_xmlrpc_server::wp_getTerms() wp-includes/class-wp-xmlrpc-server.php | Retrieve all terms for a taxonomy. |
| wp_xmlrpc_server::_insert_post() wp-includes/class-wp-xmlrpc-server.php | Helper method for wp_newPost() and wp_editPost(), containing shared logic. |
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. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_terms