get_category_by_slug() WordPress Function

The get_category_by_slug() function is used to retrieve a category object by its slug. This function is useful for retrieving a category object when you know the slug of the category, but not the ID. This function can be used in conjunction with the get_term_by() function.

get_category_by_slug( string $slug ) #

Retrieves a category object by category slug.


Parameters

$slug

(string)(Required)The category slug.


Top ↑

Return

(object|false) Category data object on success, false if not found.


Top ↑

More Information

Same is achieved by:
get_term_by('slug', $slug, 'category');


Top ↑

Source

File: wp-includes/category.php

function get_category_by_slug( $slug ) {
	$category = get_term_by( 'slug', $slug, 'category' );

	if ( $category ) {
		_make_cat_compat( $category );
	}

	return $category;
}


Top ↑

Changelog

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