Home » Classes » WP_Term_Query

If you’re working with WordPress taxonomies, then you might find the WP_Term_Query class to be useful. This class is specifically designed to query WordPress terms, which in simpler terms can be thought of as categories or tags.

The WP_Term_Query class provides a powerful and flexible way to retrieve terms based on a variety of parameters, such as term IDs, taxonomy, parent terms, and more. This class can also be used to retrieve terms based on their meta values.

One common use case for WP_Term_Query is to retrieve a list of terms for a particular post or page. This can be done by passing the post ID to the WP_Term_Query constructor, along with any additional parameters you’d like to use for filtering the results.

Here’s an example usage code for retrieving terms associated with a specific post:

$args = array(
    'object_ids' => 123, // Replace 123 with the post ID
    'taxonomy' => 'category',
    'hide_empty' => false,

$term_query = new WP_Term_Query($args);

if (!empty($term_query->terms)) {
    foreach ($term_query->terms as $term) {
        echo '<a href="' . get_term_link($term) . '">' . $term->name . '</a>';

In this example, we’re using the WP_Term_Query class to retrieve all terms from the ‘category’ taxonomy for the post with ID 123, and then displaying each term’s name and link. The get_term_link() function is used to generate the link for each term.

Overall, the WP_Term_Query class is a powerful tool for working with WordPress taxonomies, and can make it much easier to retrieve and display term data on your site.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.