Query terms with get_terms() and WP_Term_Query
get_terms() is to terms what get_posts() is to posts, and WP_Term_Query is the class behind it. Both accept the same arguments: the taxonomy, a parent, empty or not, ordering by name, count or a meta value, meta conditions, and what to return. Lists of categories in a sidebar, a menu of product categories, a filter dropdown: they all start here.
Problem: A shop page needs the top-level product categories that have products, ordered by a custom sort field, and a filter dropdown needs only IDs and names.
Solution: Call get_terms() with taxonomy, parent => 0, hide_empty, meta_key with orderby => 'meta_value_num', and fields to get only what the code uses.
Top-level categories, ordered by a field
<?php
$terms = get_terms( array(
'taxonomy' => 'product_cat',
'parent' => 0, // only top level
'hide_empty' => true, // only with published products
'meta_key' => 'ha_sort_order',
'orderby' => 'meta_value_num',
'order' => 'ASC',
) );
if ( ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
printf(
'<a href="%s">%s <span>(%d)</span></a>',
esc_url( get_term_link( $term ) ),
esc_html( $term->name ),
(int) $term->count
);
}
}
get_terms() returns a WP_Error when the taxonomy does not exist (for example when WooCommerce is deactivated), so check it before the loop. Like with posts, ordering by meta_key drops terms without that field.
Only what you need
<?php // IDs only $ids = get_terms( array( 'taxonomy' => 'category', 'fields' => 'ids', 'hide_empty' => false ) ); // id => name, ready for a select field $options = get_terms( array( 'taxonomy' => 'category', 'fields' => 'id=>name', 'hide_empty' => false ) ); // The 10 most used tags $popular = get_terms( array( 'taxonomy' => 'post_tag', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10 ) );
Other values of fields: names, slugs, id=>slug, id=>parent, count.
Hierarchy
<?php // Direct children of term 7 $children = get_terms( array( 'taxonomy' => 'product_cat', 'parent' => 7, 'hide_empty' => false ) ); // All descendants of term 7, at any depth $descendants = get_terms( array( 'taxonomy' => 'product_cat', 'child_of' => 7, 'hide_empty' => false ) ); // Only terms without children (the "leaves") $leaves = get_terms( array( 'taxonomy' => 'product_cat', 'childless' => true, 'hide_empty' => false ) );
parent is one level, child_of the whole branch. The count of a term includes only posts assigned to that term itself; with 'pad_counts' => true the count of a parent also includes the posts of all its children, which is what a category list with numbers usually should show. For a nested list in a template, wp_list_categories( array( 'taxonomy' => 'product_cat', 'child_of' => 7 ) ) prints the tree as HTML.
Meta conditions
<?php
$featured = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'meta_query' => array(
array( 'key' => 'ha_featured', 'value' => '1' ),
),
) );
How to add such fields to the term edit screen is shown in term meta for categories and custom taxonomies.
The class directly
<?php $query = new WP_Term_Query( array( 'taxonomy' => 'category', 'hide_empty' => false ) ); $terms = $query->get_terms(); // $query->request contains the SQL, useful for debugging
NOTE: number defaults to all terms. On a site with thousands of tags, an unlimited get_terms() in a sidebar loads all of them on every page view; always set a limit for lists that are shown to visitors.