WP_Term_Query Generator

Build a WP_Term_Query or get_terms() call: taxonomy, hierarchy, empty terms, ordering, meta conditions and the fields you actually need.

Live output

Enable JavaScript to customise; default output below.

Output

get_terms() is the usual way in. The dropdown option wraps wp_dropdown_categories() with the same arguments.

Live preview term-query.php
<?php
/**
 * Terms from the category taxonomy.
 */

$args = array(
	'taxonomy'   => 'category',
	'hide_empty' => true,
	'orderby'    => 'name',
	'order'      => 'ASC',
);

$terms = get_terms( $args );

// get_terms() returns a WP_Error for an unregistered taxonomy, not an array.
if ( is_wp_error( $terms ) || ! $terms ) {
	return;
}

foreach ( $terms as $term ) {
	printf(
		'<a href="%1$s">%2$s</a>',
		esc_url( get_term_link( $term ) ),
		esc_html( $term->name )
	);
}

Output is valid and updates as you type.

Build the arguments for get_terms() or WP_Term_Query, including the two settings that explain most “my term is missing” reports: hide_empty and the hierarchy.

How to use

  1. Turn hide_empty off while you are building. It defaults to true, so a term with no posts yet is simply not returned.
  2. Choose the hierarchy deliberately. parent is one level down; child_of walks the whole subtree.
  3. Ask for ids or names when that is all you need. Each full term is an object with its own meta lookup.
  4. Set a number on a taxonomy that grows, such as tags. Zero means every term, and a tag cloud of 4,000 items is a slow page.
  5. Handle the WP_Error. get_terms() returns one for a taxonomy that is not registered, which is a different shape from an empty array.

Example

Top level categories that have posts, ordered by how many:

$args = array(
	'taxonomy'   => 'category',
	'hide_empty' => true,
	'parent'     => 0,
	'orderby'    => 'count',
	'order'      => 'DESC',
);

$terms = get_terms( $args );

if ( is_wp_error( $terms ) || ! $terms ) {
	return;
}

parent => 0 is top level only. Leaving it out returns children mixed in with their parents, in name order, which reads as a broken list.

Pitfalls

  • hide_empty is true by default. A newly created term, or one only used by drafts, does not appear.
  • The count behind hide_empty is the published count for the taxonomy’s object types. A term used only on private posts counts as empty.
  • parent and child_of are different: one level versus the whole subtree. Passing both is contradictory.
  • get_terms() returns WP_Error for an unregistered taxonomy. foreach over that is a fatal.
  • number with offset paginates, but the hierarchy arguments are applied after the limit in some cases, which gives odd results on deep trees.
  • orderby => count sorts by the cached count, which can lag after a bulk import until the counts are recalculated.
  • Term meta needs WordPress 4.4. On older sites meta_key here does nothing at all.
  • get_terms() results are cached, and the cache key includes every argument. Passing a changing value, such as a timestamp, defeats it.

Compatibility

get_terms() has been in WordPress since 2.3, and since 4.5 it takes a single arguments array with taxonomy inside it. WP_Term_Query arrived in 4.6, term meta in 4.4, and wp_dropdown_categories() accepts the same arguments plus its own display ones. The generated code targets PHP 7.0 and up, and the tool runs entirely in your browser.

Frequently asked questions

Why is my new term missing?
hide_empty is on and nothing published uses the term yet. Set it to false.
parent or child_of?
parent for one level, child_of for the whole subtree beneath a term.
How do I get terms for one post?
wp_get_post_terms() or get_the_terms(). This query is for the taxonomy as a whole.
Why is get_terms() returning an error?
The taxonomy is not registered at the point you call it. Run the query on init or later, after the taxonomy exists.
Is it cached?
Yes, per argument set. Changing any argument is a different cache entry, so keep them stable.

From the people who built this tool

WP Adminify

The WordPress admin, rebuilt: a dashboard worth looking at, menu and column control, a real file manager and the login page your client sees.

See WP Adminify Free version on WordPress.org

Weekly drops

New tools, when there are new tools

One email when something worth using ships. No schedule to fill, so no filler.

Your address goes nowhere else, and one click unsubscribes.