Using WordPress ‘get_category_to_edit()’ PHP function

The get_category_to_edit() WordPress PHP function retrieves a category object for a given ID with the ‘edit’ filter context.

Usage

$edited_category = get_category_to_edit( $category_id );

Parameters

  • $id (int) – Required: The ID of the category you want to retrieve.

More information

See WordPress Developer Resources: get_category_to_edit()

Examples

Retrieve category and display its name

Retrieve a category by ID and display the category name.

$category_id = 10;
$edited_category = get_category_to_edit( $category_id );
echo 'Category Name: ' . $edited_category->name;

Update category description

Retrieve a category by ID, update its description, and save the changes.

$category_id = 15;
$new_description = 'Updated category description.';

$edited_category = get_category_to_edit( $category_id );
$edited_category->description = $new_description;

wp_update_term( $category_id, 'category', (array) $edited_category );

Add a prefix to a category name

Retrieve a category by ID, add a prefix to its name, and save the changes.

$category_id = 20;
$prefix = 'Featured: ';

$edited_category = get_category_to_edit( $category_id );
$edited_category->name = $prefix . $edited_category->name;

wp_update_term( $category_id, 'category', (array) $edited_category );

Display category slug and description

Retrieve a category by ID and display its slug and description.

$category_id = 25;
$edited_category = get_category_to_edit( $category_id );

echo 'Category Slug: ' . $edited_category->slug . '<br>';
echo 'Category Description: ' . $edited_category->description;

Change parent category

Retrieve a category by ID, change its parent category, and save the changes.

$category_id = 30;
$new_parent_id = 5;

$edited_category = get_category_to_edit( $category_id );
$edited_category->parent = $new_parent_id;

wp_update_term( $category_id, 'category', (array) $edited_category );