wp_get_object_terms

Home » Functions » wp_get_object_terms

The wp_get_object_terms function in WordPress is used to retrieve the terms associated with a specific post or taxonomy term. This function can be used to fetch the terms assigned to a post, page, or custom post type, or to get the terms assigned to a specific term in a taxonomy.

For example, if you want to display a list of categories assigned to a specific post, you can use wp_get_object_terms like this:

$post_id = get_the_ID(); // Get the current post ID
$taxonomy = 'category'; // Taxonomy name (e.g., category, post_tag)
$terms = wp_get_object_terms($post_id, $taxonomy);

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

In this example, we first get the current post ID using get_the_ID function and then retrieve the category terms associated with that post using wp_get_object_terms. Finally, we loop through the terms and display them in a list.

Learn More on WordPress.org

WordPress snippets using the wp_get_object_terms function

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