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.
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' ) );
}