How to Get a Term ID by Slug in WordPress
To get the term ID by slug in WordPress, you can use the get_term_by() function.
Here is an example code snippet:
$term_slug = 'my-term-slug';
$taxonomy = 'my-taxonomy';
$term = get_term_by('slug', $term_slug, $taxonomy);
if ($term) {
$term_id = $term->term_id;
// Use the term ID as needed
}
In the above code, replace 'my-term-slug' and 'my-taxonomy' with the actual slug and taxonomy name you want to retrieve the term ID for. The function will return a term object if it finds a match, and you can then access the term_id property of that object to get the term ID.
原文地址: https://www.cveoy.top/t/topic/oo7p 著作权归作者所有。请勿转载和采集!