Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get category object from category name

Tags:

php

wordpress

how do I get category object from category name? I can't seem to find a function that does that. if the category name is "my category". I tried running:

get_category('my category);

but it just returns 0.


1 Answers

You can use get_term_by function.

$category = get_term_by('name', 'my category', 'category')

Reference: https://codex.wordpress.org/Function_Reference/get_term_by

like image 162
Khorshed Alam Avatar answered Feb 05 '26 21:02

Khorshed Alam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!