How can I get category ID by category name?

The only alternative I know of (using core functions) is:

// Get terms whose name begins with "my_name"
get_terms( 'category', array( 'name__like' => 'my_name' ) );

// Get terms whose name contains "my_name"
get_terms( 'category', array( 'search' => 'my_name' ) );

If you need an exact match, you’ll have to execute a custom query.

$wpdb->get_results( "SELECT t.*, tt.* FROM $wpdb->terms AS t
    INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
    WHERE tt.taxonomy = 'category' AND t.name="my_name""
);

Leave a Comment