Using WordPress ‘get_all_category_ids()’ PHP function

The get_all_category_ids() WordPress PHP function retrieves all category IDs.

Usage

To use the get_all_category_ids() function, simply call it and store the result in a variable.

$all_category_ids = get_all_category_ids();

Parameters

  • None

More information

See WordPress Developer Resources: get_all_category_ids()

Examples

Displaying All Category IDs

This example shows how to display a list of all category IDs.

$all_category_ids = get_all_category_ids();
foreach ($all_category_ids as $category_id) {
    echo "Category ID: " . $category_id . "<br>";
}

Displaying All Category Names with IDs

This example displays all category names along with their IDs.

$all_category_ids = get_all_category_ids();
foreach ($all_category_ids as $category_id) {
    $category = get_category($category_id);
    echo "Category: " . $category->name . " (ID: " . $category_id . ")<br>";
}

Counting Categories

This example demonstrates how to count the total number of categories.

$all_category_ids = get_all_category_ids();
$total_categories = count($all_category_ids);
echo "Total Categories: " . $total_categories;

Displaying All Categories as Options in a Dropdown

This example shows how to create a dropdown with all categories as options.

echo "<select name='categories'>";
$all_category_ids = get_all_category_ids();
foreach ($all_category_ids as $category_id) {
    $category = get_category($category_id);
    echo "<option value='" . $category_id . "'>" . $category->name . "</option>";
}
echo "</select>";

Displaying All Categories in a UL List

This example demonstrates how to display all categories in an unordered list.

echo "<ul>";
$all_category_ids = get_all_category_ids();
foreach ($all_category_ids as $category_id) {
    $category = get_category($category_id);
    echo "<li>" . $category->name . " (ID: " . $category_id . ")</li>";
}
echo "</ul>";