Skip to content
wp-skills
Register
tools/wordpress-term-query-generator

WordPress Term Query Generator

Build a WP_Term_Query call for any taxonomy — filter by parent, search or post count, choose what the query returns, and get a loop that matches the shape it hands back.

The taxonomy slug to query. Leaving this blank would search every registered taxonomy at once.

This replaces the term objects rather than trimming them. The generated loop adapts to whichever shape you pick.

Maximum terms to return. Leave blank for all of them.

Only takes effect alongside a number — it is part of the same LIMIT clause.

Direct children of this term. 0 returns only top-level terms.

Matches term names and slugs.

snippet.php
$args = array(
	'taxonomy'     => 'category',
	'hide_empty'   => true,
	'hierarchical' => true,
	'orderby'      => 'name',
	'order'        => 'ASC',
	'number'       => 10,
);

// Passing the arguments to the constructor runs the query immediately, so
// $term_query->terms is populated by the time this returns.
$term_query = new WP_Term_Query( $args );

if ( ! empty( $term_query->terms ) ) {
	echo '<ul>';

	foreach ( $term_query->terms as $term ) {
		printf(
			'<li><a href="%s">%s (%d)</a></li>',
			esc_url( get_term_link( $term ) ),
			esc_html( $term->name ),
			(int) $term->count
		);
	}

	echo '</ul>';
} else {
	printf( '<p>%s</p>', esc_html__( 'No terms found.', 'text-domain' ) );
}