On this page
get_the_terms( int|WP_Post $post, string $taxonomy ): WP_Term[]|false|WP_Error
Retrieves the terms of the taxonomy that are attached to the post.
Parameters
$postint|WP_Post Required-
Post ID or object.
$taxonomystring Required-
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.
Source
File: wp-includes/category-template.php. View all references
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;
}
Hooks
- apply_filters( 'get_the_terms',
WP_Term[]|WP_Error $terms ,int $post_id ,string $taxonomy ) -
Filters the list of terms attached to the given post.
Related
Uses
| Uses | Description |
|---|---|
| wp_cache_add() wp-includes/cache.php | Adds data to the cache, if the cache key doesn’t already exist. |
| wp_list_pluck() wp-includes/functions.php | Plucks a certain field out of each object or array in an array. |
| get_object_term_cache() wp-includes/taxonomy.php | Retrieves the cached term objects for the given object ID. |
| wp_get_object_terms() wp-includes/taxonomy.php | Retrieves the terms associated with the given object(s), in the supplied taxonomies. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Menu_Items_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php | Prepares a single post output for response. |
| wp_set_unique_slug_on_create_template_part() wp-includes/theme-templates.php | Sets a custom slug when creating auto-draft template parts. |
| _build_block_template_result_from_post() wp-includes/block-template-utils.php | Builds a unified template object based a post Object. |
| wp_filter_wp_template_unique_post_slug() wp-includes/theme-templates.php | Generates a unique slug for templates. |
| WP_REST_Posts_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Prepares a single post output for response. |
| WP_Posts_List_Table::column_default() wp-admin/includes/class-wp-posts-list-table.php | Handles the default column output. |
| WP_Media_List_Table::column_default() wp-admin/includes/class-wp-media-list-table.php | Handles output for the default column. |
| get_the_term_list() wp-includes/category-template.php | Retrieves a post’s terms as a list with specified format. |
| get_the_tags() wp-includes/category-template.php | Retrieves the tags for a post. |
| get_the_category() wp-includes/category-template.php | Retrieves post categories. |
| get_post_class() wp-includes/post-template.php | Retrieves an array of the class names for the post container element. |
| WP_Post::__get() wp-includes/class-wp-post.php | Getter. |
| wp_publish_post() wp-includes/post.php | Publishes a post by transitioning the post status. |
| wp_update_post() wp-includes/post.php | Updates a post with new post data. |
| get_post_format() wp-includes/post-formats.php | Retrieve the format slug for a post |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_the_terms