category_exists() WordPress Function
The category_exists() WordPress function is used to check if a given category exists. It accepts a category id, slug, or name as its only parameter. If the category exists, it returns true. Otherwise, it returns false. This function is useful for making sure that a given category exists before attempting to do something with it.
category_exists( int|string $cat_name, int $category_parent = null ) #
Check whether a category exists.
Description
See also
Parameters
- $cat_name
(int|string)(Required)Category name.
- $category_parent
(int)(Optional) ID of parent category.
Default value: null
Return
(string|null) Returns the category ID as a numeric string if the pairing exists, null if not.
Source
File: wp-admin/includes/taxonomy.php
function category_exists( $cat_name, $category_parent = null ) { $id = term_exists( $cat_name, 'category', $category_parent ); if ( is_array( $id ) ) { $id = $id['term_id']; } return $id; }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
2.0.0 | Introduced. |