Retrieves the list of categories for a post.
Description
Compatibility layer for themes and plugins. Also an easy layer of abstraction away from the complexity of the taxonomy layer.
See also
Parameters
$post_id
intoptional- The Post ID. Does not default to the ID of the global $post. Default 0.
$args
arrayoptional- Category query parameters.
See WP_Term_Query::__construct() for supported arguments.Default:
array()
Source
function wp_get_post_categories( $post_id = 0, $args = array() ) {
$post_id = (int) $post_id;
$defaults = array( 'fields' => 'ids' );
$args = wp_parse_args( $args, $defaults );
$cats = wp_get_object_terms( $post_id, 'category', $args );
return $cats;
}
Changelog
Version | Description |
---|---|
2.1.0 | Introduced. |
The example below shows how categories are retrieved, and then additional information is retrieved for each category.
$post_categories = wp_get_post_categories( $post_id, array ( ‘fields’ = > ‘all’ ) );
The example below shows how categories are retrieved, and using arguments additional info will be retrieved instead of calling
get_category
Only list Category names:
List category names in Descending order:
If you want to exclude any specific term from the list you can do like: