wp_get_post_terms
Retrieve the terms for a post.
Description
(array|WP_Error) wp_get_post_terms( (int) $post_id = 0, (string) $taxonomy = 'post_tag', (array) $args = array() );
There is only one default for this function, called fields and by default is set to all.. There are other defaults that can be overridden in wp_get_object_terms(…)
.
Returns (array|WP_Error)
List of post terms or empty array if no terms were found. WP_Error object if `$taxonomy` doesn't exist.
Parameters (3)
- 0. $post_id — Optional. (int)
- The Post ID. Does not default to the ID of the global
$post
. Default 0. - 1. $taxonomy — Optional. (string) =>
'post_tag'
- The taxonomy for which to retrieve terms. Default post_tag..
- 2. $args — Optional. (array) =>
array()
-
wp_get_object_terms(…)
arguments. Default empty array.
Usage
if ( !function_exists( 'wp_get_post_terms' ) ) { require_once ABSPATH . WPINC . '/post.php'; } // Optional. The Post ID. Does not default to the ID of the // global $post. Default 0. $post_id = -1; // Optional. The taxonomy for which to retrieve terms. Default 'post_tag'. $taxonomy = 'post_tag'; // Optional. wp_get_object_terms() arguments. Default empty array. $args = array(); // NOTICE! Understand what this does before running. $result = wp_get_post_terms($post_id, $taxonomy, $args);
Defined (1)
The function is defined in the following location(s).
- /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;
- }