Get the Category ID by the Category Name

Here’s a handy function that takes the category name and returns the category id:

function get_category_id($name) {
    $term = get_term_by('name', $name, 'category');
    return $term->term_id;
}

Save this function to functions.php and call it like any other function. In this example we use it to return a list of all posts in a category named “Services”:

$posts = get_posts(array(
    'category' => get_category_id('Services')
));