How to use the get_terms() function in WordPress

Today, we’re going to take a deep dive into the inner workings of WordPress. Our purpose here is to learn all we can about the powerful function get_terms() and its many uses. You’ll come away from this post with a deeper familiarity with the get_terms() function, as well as code snippets and examples that you can use in your own WordPress development projects.

Introducing get_terms()

The get_terms() function in WordPress is a powerful tool used to retrieve the terms in a taxonomy or list of taxonomies. If you’re dealing with categories, tags, or custom taxonomies on your site, get_terms() can become your new best friend.

At its most basic, get_terms() can be called as follows:

$terms = get_terms( array( 'taxonomy' => 'category' ) );

This snippet fetches all terms within the ‘category’ taxonomy. But get_terms() has much more to offer than this basic use case, so let’s dive in!

A Closer Look at Parameters

get_terms() function accepts an array of arguments that you can use to modify the returned terms. Here’s the skeleton structure for this function:

get_terms( $args, $deprecated );

$args is an associative array of parameters that define the function’s behavior, while $deprecated is an argument for backward compatibility, and it is not in use anymore.

Let’s check out some of the most commonly used parameters:

  • 'taxonomy': An array of taxonomies from which to retrieve terms. For example, ‘category’, ‘post_tag’, or any custom taxonomy.
  • 'hide_empty': Whether to hide terms not assigned to any posts. It accepts boolean values. If you set it to true, terms with no posts will not be returned.
  • 'orderby': Sorts the order of terms returned. Possible values are ‘name’, ‘slug’, ‘term_group’, ‘term_id’, ‘id’, ‘description’, ‘count’.
  • 'order': Whether to order terms in ascending ('ASC') or descending ('DESC') order.
  • 'exclude': An array of term ids to exclude from the results.

Here’s an example of using some of these parameters:

$terms = get_terms( array(
    'taxonomy' => 'category',
    'hide_empty' => false,
    'orderby' => 'name',
    'order' => 'ASC'
) );

This will fetch all categories, including those not assigned to any posts, and order them alphabetically in ascending order.

Use Cases

Let’s examine some use cases of get_terms() to help solidify your understanding of this function.

1. Display All Categories on a Page

Here’s a snippet that retrieves all categories and echoes their names:

$categories = get_terms( array(
    'taxonomy' => 'category',
    'hide_empty' => false
) );

if ( !empty($categories) ) {
    echo '<ul>';
    foreach ( $categories as $category ) {
        echo '<li>' . $category->name . '</li>';
    }
    echo '</ul>';
}

2. Fetch Subcategories of a Specific Category

Suppose you have a category named ‘Books’, and it has several subcategories like ‘Fiction’, ‘Non-Fiction’, ‘Biographies’, etc. To fetch only the subcategories of ‘Books’, you can specify its ID using the 'parent' parameter:

$book_subcategories = get_terms( array(
    'taxonomy' => 'category',
    'parent' => 10, // Replace with the ID of 'Books' category
    'hide_empty' => false
) );

if ( !empty($book_subcategories) ) {
    echo '<ul>';
    foreach ( $book_subcategories as $category ) {
        echo '<li>' . $category->name . '</li>';
    }
    echo '</ul>';
}

3. Fetch Terms from a Custom Taxonomy

If you have a custom taxonomy named ‘genre’ for a custom post type ‘movies’, you can fetch all terms as follows:

$genres = get_terms( array(
    'taxonomy' => 'genre',
    'hide_empty' => false
) );

if ( !empty($genres) ) {
    echo '<ul>';
    foreach ( $genres as $genre ) {
        echo '<li>' . $genre->name . '</li>';
    }
    echo '</ul>';
}

Wrapping it up

In closing, WordPress’s get_terms() function is an essential tool for developers working with taxonomies. Its flexibility and depth allow it to handle a wide range of scenarios, making it a vital part of the WordPress toolkit. So go ahead, start experimenting with this function, and see how much more dynamic your WordPress sites can become!