I have a few categories with the same name [some of them are sub-categories]. And I want to get an array of ID’s for certain cattegory name.
I tried this:

 $term = get_term_by('name', $cat_name, 'category');

but it seems that get_term_by() returns only the first term that match the query.

2 Answers
2

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 Reply

Your email address will not be published. Required fields are marked *