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

Top ↑

See also


Top ↑

Parameters

$cat_name

(int|string)(Required)Category name.

$category_parent

(int)(Optional) ID of parent category.

Default value: null


Top ↑

Return

(string|null) Returns the category ID as a numeric string if the pairing exists, null if not.


Top ↑

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;
}


Top ↑

Changelog

Changelog
VersionDescription
2.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.