This function for wordpress will retrieve the category ID by the category slug. This might be useful when you access the $wp_query object directly for queried objects and the tagged slugs.
<?php
/**
* GetCatIdBySlug()
* Returns the category ID or false in case of failure
* @param string $slug
* @return mixed $query|bool
*/
function GetCatIdBySlug($slug){
if (!is_string($slug))
return false;
global $wpdb;
$query = $wpdb->get_var($wpdb->prepare("SELECT terms.term_id
FROM $wpdb->terms AS terms
INNER JOIN $wpdb->term_taxonomy AS taxonomy
ON terms.term_id = taxonomy.term_id
WHERE terms.slug = %s
AND taxonomy.taxonomy = 'category'
", $slug));
return ($query) ? $query : false;
}
?>
Mail this!
- Comments (0)
- PingBacks (0)
- TrackBacks (0)


» Latest comments