get_the_category

The timeline below displays how wordpress function get_the_category has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Retrieves post categories.
 *
 * This tag may be used outside The Loop by passing a post ID as the parameter.
 *
 * Note: This function only returns results from the default "category" taxonomy.
 * For custom taxonomies use get_the_terms().
 *
 * @since 0.71
 *
 * @param int $post_id Optional. The post ID. Defaults to current post ID.
 * @return WP_Term[] Array of WP_Term objects, one for each category assigned to the post.
 */
function get_the_category($post_id = false)
{
    $categories = get_the_terms($post_id, 'category');
    if (!$categories || is_wp_error($categories)) {
        $categories = array();
    }
    $categories = array_values($categories);
    foreach (array_keys($categories) as $key) {
        _make_cat_compat($categories[$key]);
    }
    /**
     * Filters the array of categories to return for a post.
     *
     * @since 3.1.0
     * @since 4.4.0 Added the `$post_id` parameter.
     *
     * @param WP_Term[] $categories An array of categories to return for the post.
     * @param int|false $post_id    The post ID.
     */
    return apply_filters('get_the_categories', $categories, $post_id);
}

WordPress Version: 5.5

/**
 * Retrieves post categories.
 *
 * This tag may be used outside The Loop by passing a post ID as the parameter.
 *
 * Note: This function only returns results from the default "category" taxonomy.
 * For custom taxonomies use get_the_terms().
 *
 * @since 0.71
 *
 * @param int $post_id Optional. The post ID. Defaults to current post ID.
 * @return WP_Term[] Array of WP_Term objects, one for each category assigned to the post.
 */
function get_the_category($post_id = false)
{
    $categories = get_the_terms($post_id, 'category');
    if (!$categories || is_wp_error($categories)) {
        $categories = array();
    }
    $categories = array_values($categories);
    foreach (array_keys($categories) as $key) {
        _make_cat_compat($categories[$key]);
    }
    /**
     * Filters the array of categories to return for a post.
     *
     * @since 3.1.0
     * @since 4.4.0 Added `$post_id` parameter.
     *
     * @param WP_Term[] $categories An array of categories to return for the post.
     * @param int|false $post_id    ID of the post.
     */
    return apply_filters('get_the_categories', $categories, $post_id);
}

WordPress Version: 5.1

/**
 * Retrieve post categories.
 *
 * This tag may be used outside The Loop by passing a post id as the parameter.
 *
 * Note: This function only returns results from the default "category" taxonomy.
 * For custom taxonomies use get_the_terms().
 *
 * @since 0.71
 *
 * @param int $id Optional, default to current post ID. The post ID.
 * @return WP_Term[] Array of WP_Term objects, one for each category assigned to the post.
 */
function get_the_category($id = false)
{
    $categories = get_the_terms($id, 'category');
    if (!$categories || is_wp_error($categories)) {
        $categories = array();
    }
    $categories = array_values($categories);
    foreach (array_keys($categories) as $key) {
        _make_cat_compat($categories[$key]);
    }
    /**
     * Filters the array of categories to return for a post.
     *
     * @since 3.1.0
     * @since 4.4.0 Added `$id` parameter.
     *
     * @param WP_Term[] $categories An array of categories to return for the post.
     * @param int|false $id         ID of the post.
     */
    return apply_filters('get_the_categories', $categories, $id);
}

WordPress Version: 4.6

/**
 * Retrieve post categories.
 *
 * This tag may be used outside The Loop by passing a post id as the parameter.
 *
 * Note: This function only returns results from the default "category" taxonomy.
 * For custom taxonomies use get_the_terms().
 *
 * @since 0.71
 *
 * @param int $id Optional, default to current post ID. The post ID.
 * @return array Array of WP_Term objects, one for each category assigned to the post.
 */
function get_the_category($id = false)
{
    $categories = get_the_terms($id, 'category');
    if (!$categories || is_wp_error($categories)) {
        $categories = array();
    }
    $categories = array_values($categories);
    foreach (array_keys($categories) as $key) {
        _make_cat_compat($categories[$key]);
    }
    /**
     * Filters the array of categories to return for a post.
     *
     * @since 3.1.0
     * @since 4.4.0 Added `$id` parameter.
     *
     * @param array $categories An array of categories to return for the post.
     * @param int   $id         ID of the post.
     */
    return apply_filters('get_the_categories', $categories, $id);
}

WordPress Version: 4.4

/**
 * Retrieve post categories.
 *
 * This tag may be used outside The Loop by passing a post id as the parameter.
 *
 * Note: This function only returns results from the default "category" taxonomy.
 * For custom taxonomies use get_the_terms().
 *
 * @since 0.71
 *
 * @param int $id Optional, default to current post ID. The post ID.
 * @return array Array of objects, one for each category assigned to the post.
 */
function get_the_category($id = false)
{
    $categories = get_the_terms($id, 'category');
    if (!$categories || is_wp_error($categories)) {
        $categories = array();
    }
    $categories = array_values($categories);
    foreach (array_keys($categories) as $key) {
        _make_cat_compat($categories[$key]);
    }
    /**
     * Filter the array of categories to return for a post.
     *
     * @since 3.1.0
     * @since 4.4.0 Added `$id` parameter.
     *
     * @param array $categories An array of categories to return for the post.
     * @param int   $id         ID of the post.
     */
    return apply_filters('get_the_categories', $categories, $id);
}

WordPress Version: 4.3

/**
 * Retrieve post categories.
 *
 * This tag may be used outside The Loop by passing a post id as the parameter.
 *
 * Note: This function only returns results from the default "category" taxonomy.
 * For custom taxonomies use get_the_terms().
 *
 * @since 0.71
 *
 * @param int $id Optional, default to current post ID. The post ID.
 * @return array Array of objects, one for each category assigned to the post.
 */
function get_the_category($id = false)
{
    $categories = get_the_terms($id, 'category');
    if (!$categories || is_wp_error($categories)) {
        $categories = array();
    }
    $categories = array_values($categories);
    foreach (array_keys($categories) as $key) {
        _make_cat_compat($categories[$key]);
    }
    /**
     * Filter the array of categories to return for a post.
     *
     * @since 3.1.0
     *
     * @param array $categories An array of categories to return for the post.
     */
    return apply_filters('get_the_categories', $categories);
}

WordPress Version: 4.1

/**
 * Retrieve post categories.
 *
 * @since 0.71
 *
 * @param int $id Optional, default to current post ID. The post ID.
 * @return array
 */
function get_the_category($id = false)
{
    $categories = get_the_terms($id, 'category');
    if (!$categories || is_wp_error($categories)) {
        $categories = array();
    }
    $categories = array_values($categories);
    foreach (array_keys($categories) as $key) {
        _make_cat_compat($categories[$key]);
    }
    /**
     * Filter the array of categories to return for a post.
     *
     * @since 3.1.0
     *
     * @param array $categories An array of categories to return for the post.
     */
    return apply_filters('get_the_categories', $categories);
}

WordPress Version: 3.9

/**
 * Retrieve post categories.
 *
 * @since 0.71
 * @uses $post
 *
 * @param int $id Optional, default to current post ID. The post ID.
 * @return array
 */
function get_the_category($id = false)
{
    $categories = get_the_terms($id, 'category');
    if (!$categories || is_wp_error($categories)) {
        $categories = array();
    }
    $categories = array_values($categories);
    foreach (array_keys($categories) as $key) {
        _make_cat_compat($categories[$key]);
    }
    /**
     * Filter the array of categories to return for a post.
     *
     * @since 3.1.0
     *
     * @param array $categories An array of categories to return for the post.
     */
    return apply_filters('get_the_categories', $categories);
}

WordPress Version: 3.7

/**
 * Retrieve post categories.
 *
 * @since 0.71
 * @uses $post
 *
 * @param int $id Optional, default to current post ID. The post ID.
 * @return array
 */
function get_the_category($id = false)
{
    $categories = get_the_terms($id, 'category');
    if (!$categories || is_wp_error($categories)) {
        $categories = array();
    }
    $categories = array_values($categories);
    foreach (array_keys($categories) as $key) {
        _make_cat_compat($categories[$key]);
    }
    // Filter name is plural because we return alot of categories (possibly more than #13237) not just one
    return apply_filters('get_the_categories', $categories);
}