Using WordPress ‘get_categories()’ PHP function

The get_categories() WordPress PHP function retrieves a list of category objects based on the given arguments.

Usage

$categories = get_categories($args);

Parameters

  • $args (string|array) – Optional. Arguments to retrieve categories. See get_terms() for additional options.

More information

See WordPress Developer Resources: get_categories()

Examples

List Categories and Descriptions

This example lists all categories alphabetically and shows their descriptions and post counts.

$categories = get_categories(array('orderby' => 'name', 'order' => 'ASC'));

foreach ($categories as $category) {
    // ... (see full example in the question)
}

Get Both Used and Unused Categories

This example returns categories that are both in use (assigned to posts) and not in use (not assigned to any posts).

$args = array('hide_empty' => false);
$all_categories = get_categories($args);

Get Only Top Level Categories

This example gets the links and names of top-level categories.

$categories = get_categories(array('orderby' => 'name', 'parent' => 0));

foreach ($categories as $category) {
    // ... (see full example in the question)
}

Display Specific Category Parent Title with Linked Menu of Subcategories

This example shows a specific category parent title with a linked menu of its subcategories.

// ... (see full example in the question)

Get Categories with Post Count Greater Than 5

This example retrieves categories that have more than 5 posts.

$categories = get_categories();

foreach ($categories as $category) {
    if ($category->count > 5) {
        // ... (do something with the category)
    }
}