WordPress at a glance

get_the_terms() WP 1.0

Retrieve the terms of the taxonomy that are attached to the post.

Works based on: wp_get_object_terms()
✈ 1 time = 0.005139s = very slow | 50000 times = 1.66s = fast PHP 7.1.2, WP 4.8
Hooks from function:
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.

Usage

get_the_terms( $post, $taxonomy );
$post(int/object) (required)
Post ID or object.
$taxonomy(string) (required)
Taxonomy name.

Code of get_the_terms: wp-includes/category-template.php VER 4.9.8

<?php
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;
}

Related Functions

From tag: Term (taxonomies terms)

More from category: For posts

No comments
    Hello, !     Log In . Register