Get Term ID by Description
-
14-04-2021 - |
Question
I need to return the taxonomy term ID based on the terms description. Is this possible?
I know the description along with the term ID is held within the wp_term_taxonomy
table. However I was wondering whether I could retrieve it through a function rather than having to use wpdb.
Solution
I'm not aware of ready-made functions that support this, but you can easily construct a WP_Term_Query
to do this. The following code is untested but should work:
$args = [
'description__like' => 'the description you\'re searching for',
'taxonomy' => 'category',//or other taxonomy
'fields' => 'ids',
'hide_empty' => false,
];
$term_query = new WP_Term_Query($args);
foreach ($term_query->terms as $term) {
// ID is in $term->term_id
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange