Using WordPress ‘list_cats’ PHP filter

The list_cats WordPress PHP Filter allows you to modify taxonomy drop-down display elements just before they are displayed. You can filter arguments such as ‘show_option_none’, ‘show_option_all’, and various forms of the term name.

Usage

add_filter('list_cats', 'your_custom_function_name', 10, 2);

function your_custom_function_name($element, $category) {
    // your custom code here
    return $element;
}

Parameters

  • $element (string): Category name.
  • $category (WP_Term|null): The category object, or null if there’s no corresponding category.

More information

See WordPress Developer Resources: list_cats

Examples

Add a custom prefix to category names

This example adds the text “Category: ” as a prefix to each category name.

add_filter('list_cats', 'add_prefix_to_category_names', 10, 2);

function add_prefix_to_category_names($element, $category) {
    $element = 'Category: ' . $element;
    return $element;
}

Display category ID next to category name

Display the category ID next to the category name in the drop-down.

add_filter('list_cats', 'display_category_id', 10, 2);

function display_category_id($element, $category) {
    if ($category) {
        $element .= ' (' . $category->term_id . ')';
    }
    return $element;
}

Uppercase category names

Transform all category names to uppercase in the drop-down.

add_filter('list_cats', 'uppercase_category_names', 10, 2);

function uppercase_category_names($element, $category) {
    $element = strtoupper($element);
    return $element;
}

Show category count next to category name

Display the number of posts in each category next to the category name.

add_filter('list_cats', 'show_category_count', 10, 2);

function show_category_count($element, $category) {
    if ($category) {
        $element .= ' (' . $category->count . ')';
    }
    return $element;
}

Remove empty categories

Hide categories with no posts from the drop-down.

add_filter('list_cats', 'hide_empty_categories', 10, 2);
function hide_empty_categories($element, $category) {
    if ($category && $category->count > 0) {
        return $element;
    }
    return '';
}