get_the_terms

The timeline below displays how wordpress function get_the_terms has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.5

/**
 * Retrieves the terms of the taxonomy that are attached to the post.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post     Post ID or object.
 * @param string      $taxonomy Taxonomy name.
 * @return WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms
 *                                  or the post does not exist, WP_Error on failure.
 */
function get_the_terms($post, $taxonomy)
{
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    $terms = get_object_term_cache($post->ID, $taxonomy);
    if (false === $terms) {
        $terms = wp_get_object_terms($post->ID, $taxonomy);
        if (!is_wp_error($terms)) {
            $term_ids = wp_list_pluck($terms, 'term_id');
            wp_cache_add($post->ID, $term_ids, $taxonomy . '_relationships');
        }
    }
    /**
     * Filters the list of terms attached to the given post.
     *
     * @since 3.1.0
     *
     * @param WP_Term[]|WP_Error $terms    Array of attached terms, or WP_Error on failure.
     * @param int                $post_id  Post ID.
     * @param string             $taxonomy Name of the taxonomy.
     */
    $terms = apply_filters('get_the_terms', $terms, $post->ID, $taxonomy);
    if (empty($terms)) {
        return false;
    }
    return $terms;
}

WordPress Version: 5.3

/**
 * Retrieve the terms of the taxonomy that are attached to the post.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post     Post ID or object.
 * @param string      $taxonomy Taxonomy name.
 * @return WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms
 *                                  or the post does not exist, WP_Error on failure.
 */
function get_the_terms($post, $taxonomy)
{
    $post = get_post($post);
    if (!$post) {
        return false;
    }
    $terms = get_object_term_cache($post->ID, $taxonomy);
    if (false === $terms) {
        $terms = wp_get_object_terms($post->ID, $taxonomy);
        if (!is_wp_error($terms)) {
            $term_ids = wp_list_pluck($terms, 'term_id');
            wp_cache_add($post->ID, $term_ids, $taxonomy . '_relationships');
        }
    }
    /**
     * Filters the list of terms attached to the given post.
     *
     * @since 3.1.0
     *
     * @param WP_Term[]|WP_Error $terms    Array of attached terms, or WP_Error on failure.
     * @param int                $post_id  Post ID.
     * @param string             $taxonomy Name of the taxonomy.
     */
    $terms = apply_filters('get_the_terms', $terms, $post->ID, $taxonomy);
    if (empty($terms)) {
        return false;
    }
    return $terms;
}

WordPress Version: 5.1

/**
 * Retrieve the terms of the taxonomy that are attached to the post.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post     Post ID or object.
 * @param string      $taxonomy Taxonomy name.
 * @return WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms
 *                                  or the post does not exist, WP_Error on failure.
 */
function get_the_terms($post, $taxonomy)
{
    if (!$post = get_post($post)) {
        return false;
    }
    $terms = get_object_term_cache($post->ID, $taxonomy);
    if (false === $terms) {
        $terms = wp_get_object_terms($post->ID, $taxonomy);
        if (!is_wp_error($terms)) {
            $term_ids = wp_list_pluck($terms, 'term_id');
            wp_cache_add($post->ID, $term_ids, $taxonomy . '_relationships');
        }
    }
    /**
     * Filters the list of terms attached to the given post.
     *
     * @since 3.1.0
     *
     * @param WP_Term[]|WP_Error $terms    Array of attached terms, or WP_Error on failure.
     * @param int                $post_id  Post ID.
     * @param string             $taxonomy Name of the taxonomy.
     */
    $terms = apply_filters('get_the_terms', $terms, $post->ID, $taxonomy);
    if (empty($terms)) {
        return false;
    }
    return $terms;
}

WordPress Version: 4.6

/**
 * Retrieve the terms of the taxonomy that are attached to the post.
 *
 * @since 2.5.0
 *
 * @param int|object $post Post ID or object.
 * @param string $taxonomy Taxonomy name.
 * @return array|false|WP_Error Array of WP_Term objects on success, false if there are no terms
 *                              or the post does not exist, WP_Error on failure.
 */
function get_the_terms($post, $taxonomy)
{
    if (!$post = get_post($post)) {
        return false;
    }
    $terms = get_object_term_cache($post->ID, $taxonomy);
    if (false === $terms) {
        $terms = wp_get_object_terms($post->ID, $taxonomy);
        if (!is_wp_error($terms)) {
            $term_ids = wp_list_pluck($terms, 'term_id');
            wp_cache_add($post->ID, $term_ids, $taxonomy . '_relationships');
        }
    }
    /**
     * Filters the list of terms attached to the given post.
     *
     * @since 3.1.0
     *
     * @param array|WP_Error $terms    List of attached terms, or WP_Error on failure.
     * @param int            $post_id  Post ID.
     * @param string         $taxonomy Name of the taxonomy.
     */
    $terms = apply_filters('get_the_terms', $terms, $post->ID, $taxonomy);
    if (empty($terms)) {
        return false;
    }
    return $terms;
}

WordPress Version: 4.1

/**
 * Retrieve the terms of the taxonomy that are attached to the post.
 *
 * @since 2.5.0
 *
 * @param int|object $post Post ID or object.
 * @param string $taxonomy Taxonomy name.
 * @return array|false|WP_Error Array of term objects on success, false if there are no terms
 *                              or the post does not exist, WP_Error on failure.
 */
function get_the_terms($post, $taxonomy)
{
    if (!$post = get_post($post)) {
        return false;
    }
    $terms = get_object_term_cache($post->ID, $taxonomy);
    if (false === $terms) {
        $terms = wp_get_object_terms($post->ID, $taxonomy);
        if (!is_wp_error($terms)) {
            $to_cache = array();
            foreach ($terms as $key => $term) {
                $to_cache[$key] = $term->data;
            }
            wp_cache_add($post->ID, $to_cache, $taxonomy . '_relationships');
        }
    }
    if (!is_wp_error($terms)) {
        $terms = array_map('get_term', $terms);
    }
    /**
     * Filter the list of terms attached to the given post.
     *
     * @since 3.1.0
     *
     * @param array|WP_Error $terms    List of attached terms, or WP_Error on failure.
     * @param int            $post_id  Post ID.
     * @param string         $taxonomy Name of the taxonomy.
     */
    $terms = apply_filters('get_the_terms', $terms, $post->ID, $taxonomy);
    if (empty($terms)) {
        return false;
    }
    return $terms;
}

WordPress Version: 4.4

/**
 * Retrieve the terms of the taxonomy that are attached to the post.
 *
 * @since 2.5.0
 *
 * @param int|object $post Post ID or object.
 * @param string $taxonomy Taxonomy name.
 * @return array|false|WP_Error Array of term objects on success, false if there are no terms
 *                              or the post does not exist, WP_Error on failure.
 */
function get_the_terms($post, $taxonomy)
{
    if (!$post = get_post($post)) {
        return false;
    }
    $terms = get_object_term_cache($post->ID, $taxonomy);
    if (false === $terms) {
        $terms = wp_get_object_terms($post->ID, $taxonomy);
        $to_cache = array();
        foreach ($terms as $key => $term) {
            $to_cache[$key] = $term->data;
        }
        wp_cache_add($post->ID, $to_cache, $taxonomy . '_relationships');
    }
    $terms = array_map('get_term', $terms);
    /**
     * Filter the list of terms attached to the given post.
     *
     * @since 3.1.0
     *
     * @param array|WP_Error $terms    List of attached terms, or WP_Error on failure.
     * @param int            $post_id  Post ID.
     * @param string         $taxonomy Name of the taxonomy.
     */
    $terms = apply_filters('get_the_terms', $terms, $post->ID, $taxonomy);
    if (empty($terms)) {
        return false;
    }
    return $terms;
}

WordPress Version: 4.3

/**
 * Retrieve the terms of the taxonomy that are attached to the post.
 *
 * @since 2.5.0
 *
 * @param int|object $post Post ID or object.
 * @param string $taxonomy Taxonomy name.
 * @return array|false|WP_Error Array of term objects on success, false if there are no terms
 *                              or the post does not exist, WP_Error on failure.
 */
function get_the_terms($post, $taxonomy)
{
    if (!$post = get_post($post)) {
        return false;
    }
    $terms = get_object_term_cache($post->ID, $taxonomy);
    if (false === $terms) {
        $terms = wp_get_object_terms($post->ID, $taxonomy);
        wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
    }
    /**
     * Filter the list of terms attached to the given post.
     *
     * @since 3.1.0
     *
     * @param array|WP_Error $terms    List of attached terms, or WP_Error on failure.
     * @param int            $post_id  Post ID.
     * @param string         $taxonomy Name of the taxonomy.
     */
    $terms = apply_filters('get_the_terms', $terms, $post->ID, $taxonomy);
    if (empty($terms)) {
        return false;
    }
    return $terms;
}

WordPress Version: 4.0

/**
 * Retrieve the terms of the taxonomy that are attached to the post.
 *
 * @since 2.5.0
 *
 * @param int|object $post Post ID or object.
 * @param string $taxonomy Taxonomy name.
 * @return array|bool|WP_Error Array of term objects on success, false if there are no terms
 *                             or the post does not exist, WP_Error on failure.
 */
function get_the_terms($post, $taxonomy)
{
    if (!$post = get_post($post)) {
        return false;
    }
    $terms = get_object_term_cache($post->ID, $taxonomy);
    if (false === $terms) {
        $terms = wp_get_object_terms($post->ID, $taxonomy);
        wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
    }
    /**
     * Filter the list of terms attached to the given post.
     *
     * @since 3.1.0
     *
     * @param array|WP_Error $terms    List of attached terms, or WP_Error on failure.
     * @param int            $post_id  Post ID.
     * @param string         $taxonomy Name of the taxonomy.
     */
    $terms = apply_filters('get_the_terms', $terms, $post->ID, $taxonomy);
    if (empty($terms)) {
        return false;
    }
    return $terms;
}

WordPress Version: 3.9

/**
 * Retrieve the terms of the taxonomy that are attached to the post.
 *
 * @since 2.5.0
 *
 * @param int|object $post Post ID or object.
 * @param string $taxonomy Taxonomy name.
 * @return array|bool|WP_Error Array of term objects on success, false or WP_Error on failure.
 */
function get_the_terms($post, $taxonomy)
{
    if (!$post = get_post($post)) {
        return false;
    }
    $terms = get_object_term_cache($post->ID, $taxonomy);
    if (false === $terms) {
        $terms = wp_get_object_terms($post->ID, $taxonomy);
        wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
    }
    /**
     * Filter the list of terms attached to the given post.
     *
     * @since 3.1.0
     *
     * @param array  $terms    List of attached terms.
     * @param int    $post_id  Post ID.
     * @param string $taxonomy Name of the taxonomy.
     */
    $terms = apply_filters('get_the_terms', $terms, $post->ID, $taxonomy);
    if (empty($terms)) {
        return false;
    }
    return $terms;
}

WordPress Version: 3.7

/**
 * Retrieve the terms of the taxonomy that are attached to the post.
 *
 * @since 2.5.0
 *
 * @param int|object $post Post ID or object.
 * @param string $taxonomy Taxonomy name.
 * @return array|bool|WP_Error Array of term objects on success, false or WP_Error on failure.
 */
function get_the_terms($post, $taxonomy)
{
    if (!$post = get_post($post)) {
        return false;
    }
    $terms = get_object_term_cache($post->ID, $taxonomy);
    if (false === $terms) {
        $terms = wp_get_object_terms($post->ID, $taxonomy);
        wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
    }
    $terms = apply_filters('get_the_terms', $terms, $post->ID, $taxonomy);
    if (empty($terms)) {
        return false;
    }
    return $terms;
}