get_the_terms ( int|object $post, string $taxonomy )

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

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.

Source file: wp-includes/category-template.php

View source


Parameters

$post

(int|object) (Required) Post ID or object.

$taxonomy

(string) (Required) Taxonomy name.


Used by


Source

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


Changelog


User Contributed Notes

  1. A Basic Example

    Echoing the list of terms (for a taxonomy called on-draught). This is similar to the output from get_the_term_list, but without the terms being hyperlinked:

    $terms = get_the_terms( get_the_ID(), 'on-draught' );
    						
    if ( $terms && ! is_wp_error( $terms ) ) : 
    
    	$draught_links = array();
    
    	foreach ( $terms as $term ) {
    		$draught_links[] = $term->name;
    	}
    						
    	$on_draught = join( ", ", $draught_links );
    	?>
    
    	<p class="beers draught">
    		<?php printf( esc_html__( 'On draught: <span>%s</span>', 'textdomain' ), esc_html( $on_draught ) ); ?>
    	</p>
    <?php endif; ?>
    
  2. Get terms for all custom taxonomies

    Place this function in your theme’s functions.php.

    /**
     * Get taxonomies terms links.
     *
     * @see get_object_taxonomies()
     */
    function wpdocs_custom_taxonomies_terms_links() {
    	// Get post by post ID.
    	$post = get_post( $post->ID );
    
    	// Get post type by post.
    	$post_type = $post->post_type;
    
    	// Get post type taxonomies.
    	$taxonomies = get_object_taxonomies( $post_type, 'objects' );
    
    	$out = array();
    
    	foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
    
    		// Get the terms related to post.
    		$terms = get_the_terms( $post->ID, $taxonomy_slug );
    
    		if ( ! empty( $terms ) ) {
    			$out[] = "<h2>" . $taxonomy->label . "</h2>\n<ul>";
    			foreach ( $terms as $term ) {
    				$out[] = sprintf( '<li><a href="%1$s">%2$s</a></li>',
    					esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
    					esc_html( $term->name )
    				);
    			}
    			$out[] = "\n</ul>\n";
    		}
    	}
    	return implode( '', $out );
    }
    ?>
    

    Now you can use this function in your theme:

    <?php echo wpdocs_custom_taxonomies_terms_links(); ?>
    

You must log in before being able to contribute a note.