get_cat_ID() WordPress Function

The get_cat_ID() function in WordPress returns the category ID for a given category name. It is useful for getting the ID of a category when you only have the name.

get_cat_ID( string $cat_name ) #

Retrieves the ID of a category from its name.


Parameters

$cat_name

(string)(Required)Category name.


Top ↑

Return

(int) Category ID on success, 0 if the category doesn't exist.


Top ↑

Source

File: wp-includes/category.php

function get_cat_ID( $cat_name ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
	$cat = get_term_by( 'name', $cat_name, 'category' );

	if ( $cat ) {
		return $cat->term_id;
	}

	return 0;
}


Top ↑

Changelog

Changelog
VersionDescription
1.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.