wp_get_post_terms( int $post_id, string|string[] $taxonomy = 'post_tag', array $args = array() )
Retrieves the terms for a post.
Parameters
- $post_id
-
(int) (Optional) The Post ID. Does not default to the ID of the global $post. Default 0.
- $taxonomy
-
(string|string[]) (Optional) The taxonomy slug or array of slugs for which to retrieve terms.
Default value: 'post_tag'
- $args
-
(array) (Optional) Term query parameters. See WP_Term_Query::__construct() for supported arguments.
- 'fields'
(string) Term fields to retrieve. Default 'all'.
Default value: array()
- 'fields'
Return
(array|WP_Error) Array of WP_Term objects on success or empty array if no terms were found. WP_Error object if $taxonomy
doesn't exist.
Source
File: wp-includes/post.php
function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) { $post_id = (int) $post_id; $defaults = array( 'fields' => 'all' ); $args = wp_parse_args( $args, $defaults ); $tags = wp_get_object_terms( $post_id, $taxonomy, $args ); return $tags; }
Expand full source code Collapse full source code View on Trac View on GitHub
Changelog
Version | Description |
---|---|
2.8.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
The difference between this function and
get_the_terms()
is that this function’s results are not cached (thus, it’ll hit the database every time its called).Top ↑
Feedback
This is not entirely true. Another key distinction is that
get_the_terms()
does not allow the third parameter:$args
. — By Joshua Fredrickson —Examples
Get term category to only run function if the post (CPT) has that category:
Expand full source codeCollapse full source code
Use curly brackets recommended but not needed on one expression of foreach
Get all taxonomy slug of a post
Expand full source codeCollapse full source code
You can get the taxonomy terms HTML using this function am_get_post_term