Home > AI > Backend > Wordpress >

get the category id by its name

// Get terms whose name begins with "ios"
<?php
$a = get_terms( 'st_ai_cat', array( 'name__like' => 'ios' ) );
var_dump($a[0]->term_id);
?>

// Get terms whose name contains "my_name"
<?php
$a = get_terms( 'st_ai_cat', array( 'search' => 'ios' ) );
var_dump($a[0]->term_id);
?>

// exact match
$a = $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 = 'st_ai_cat' AND t.name = 'ios'"
);
var_dump($a[0]->term_id);

Leave a Reply