Using WordPress ‘get_the_categories’ PHP function

The get_the_categories WordPress PHP filter allows you to modify the array of categories returned for a specific post.

Usage

add_filter('get_the_categories', 'your_custom_function', 10, 2);

function your_custom_function($categories, $post_id) {
    // your custom code here
    return $categories;
}

Parameters

  • $categories (WP_Term[]): An array of categories to return for the post.
  • $post_id (int|false): The post ID.

More information

See WordPress Developer Resources: get_the_categories

Examples

Exclude a specific category

Exclude the “Uncategorized” category from the list of categories for a post.

add_filter('get_the_categories', 'exclude_uncategorized_category', 10, 2);

function exclude_uncategorized_category($categories, $post_id) {
    return array_filter($categories, function($category) {
        return $category->slug !== 'uncategorized';
    });
}

Sort categories alphabetically

Sort the categories returned for a post alphabetically by their name.

add_filter('get_the_categories', 'sort_categories_alphabetically', 10, 2);

function sort_categories_alphabetically($categories, $post_id) {
    usort($categories, function($a, $b) {
        return strcmp($a->name, $b->name);
    });
    return $categories;
}

Limit the number of categories displayed

Limit the number of categories displayed for a post to 3.

add_filter('get_the_categories', 'limit_category_count', 10, 2);

function limit_category_count($categories, $post_id) {
    return array_slice($categories, 0, 3);
}

Add a custom prefix to category names

Add the prefix “Topic: ” to each category name.

add_filter('get_the_categories', 'add_category_prefix', 10, 2);

function add_category_prefix($categories, $post_id) {
    return array_map(function($category) {
        $category->name = 'Topic: ' . $category->name;
        return $category;
    }, $categories);
}

Remove parent categories

Display only child categories, excluding parent categories.

add_filter('get_the_categories', 'remove_parent_categories', 10, 2);
function remove_parent_categories($categories, $post_id) {
return array_filter($categories, function($category) {
return $category->parent !== 0;
});
}

Exclude a specific category

Exclude the category with the ID of 5 from the list of categories.

add_filter('get_the_categories', 'exclude_specific_category', 10, 2);

function exclude_specific_category($categories, $post_id) {
  $filtered_categories = array_filter($categories, function($category) {
    return $category->term_id != 5;
  });
  return $filtered_categories;
}

Order categories alphabetically

Order the categories alphabetically by their name.

add_filter('get_the_categories', 'order_categories_alphabetically', 10, 2);

function order_categories_alphabetically($categories, $post_id) {
  usort($categories, function($a, $b) {
    return strcmp($a->name, $b->name);
  });
  return $categories;
}

Limit the number of categories

Limit the number of categories returned to 3.

add_filter('get_the_categories', 'limit_categories', 10, 2);

function limit_categories($categories, $post_id) {
  return array_slice($categories, 0, 3);
}

Add a prefix to category names

Add the prefix “Category:” to each category name.

add_filter('get_the_categories', 'prefix_category_names', 10, 2);

function prefix_category_names($categories, $post_id) {
  foreach ($categories as $category) {
    $category->name = 'Category: ' . $category->name;
  }
  return $categories;
}

Remove empty categories

Remove categories that have no posts assigned to them.

add_filter('get_the_categories', 'remove_empty_categories', 10, 2);

function remove_empty_categories($categories, $post_id) {
  $filtered_categories = array_filter($categories, function($category) {
    return $category->count > 0;
  });
  return $filtered_categories;
}