<p>Depending on what you are searching by, you can use something similar to this. You can see the properties you can search by <a href="http://buildwithcraft.com/docs/templating/craft.categories" rel="nofollow">in the documentation</a>.</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
$criteria-&gt;group = 'CategoryGroupName';
$criteria-&gt;title = 'CategoryTitle';
$criteria-&gt;limit = 1;

$category = $criteria-&gt;find();
</code></pre>

CC BY-SA 3.0
2015-04-20T16:37:57.547Z
Doesn't **craft()->elements->getCriteria(ElementType::Category);** technically load all categories? If so I want to avoid the overhead.
Matt V
CC BY-SA 3.0
2015-04-20T16:41:41.430Z
Nope! It loads an element criteria model of type Category. That simply allows you to query against Category properties like group, title, level, etc. ([source](https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/ElementsService.php#L33))
Aaron Berkowitz
CC BY-SA 3.0
2015-04-20T16:47:54.780Z
Oh ok. Cool. I'll give this a try.
Matt V
CC BY-SA 3.0
2015-04-28T20:14:10.727Z
Question: How would I get only the children of a given category? What I mean is... I have a list of countries and under them their states/provinces. Is it possible to do something like **$criteria->children = 1**? I've tried this and it doesn't work.
Matt V
CC BY-SA 3.0
2015-04-28T20:20:19.313Z
You can limit the search to what [`level`](https://buildwithcraft.com/docs/templating/craft.categories#level) the category is. So if you know you just want categories of the second level (states/provinces), you can add `$criteria->level = 2` before you run `find()`
Aaron Berkowitz
CC BY-SA 3.0
2015-04-29T13:07:10.850Z
I've done that but I also get the states/provinces from other countries. I assume that **$criteria->level = 2** gets all level 2 items regardless of the parent. At least that has been my experience.
Matt V
CC BY-SA 3.0
2015-04-29T13:09:52.547Z
That's correct. You can limit your search even more if you have the country you want the states from by adding `$criteria->descendantOf = $country`
Aaron Berkowitz
CC BY-SA 3.0
2015-04-29T13:22:05.073Z
Oh ok. It seems to me then that I have to first get the country, then use that data to get the descendantOf. Is there a way to do this all in one shot or do I need to get country then use that result on a second query?
Matt V
CC BY-SA 3.0
2015-04-29T15:02:02.000Z
Yeah, you will need to first get the parent category, then get the children in two separate finds. If you are going to be running these steps frequently, I highly recommend cacheing the process. You can see a simple cache example in [this service](https://github.com/aberkie/craft-summit-api/blob/master/disney/services/Disney_InterestService.php) I made for the Craft CMS summit.
Aaron Berkowitz
CC BY-SA 3.0
2015-04-29T17:42:37.560Z
No problem! If this is working for you, can you select the answer as the correct answer so we can close out your question?
Aaron Berkowitz
CC BY-SA 3.0
2015-04-29T19:03:33.660Z
Not really sure how to do that, but I marked it as userful
Matt V
CC BY-SA 3.0
2015-04-20T16:32:19.220Z
2015-04-20T16:49:25.640Z
2015-04-20T16:49:25.640Z
Aaron Berkowitz
<p>I have a category that I'm trying to load via a plugin. Looking at the categories service I see there is a function called "getCategoryById" but I don't know what the ID is so instead I'm loading the category group thinking that the ID provided there is what I need, apparently it is not.</p>

<p>Kinda of stumped on this one. Any advice?</p>

<p>Thanks,</p>

CC BY-SA 3.0
2015-04-20T16:28:45.887Z
Hey Matt - what infomormation do you have about the category? Name, level, slug?
Aaron Berkowitz
<plugin-development><categories>
Load Category in Plugin
# Load Category in Plugin

(Asked by: username_0 on 2015-04-20)

I have a category that I'm trying to load via a plugin. Looking at the categories service I see there is a function called "getCategoryById" but I don't know what the ID is so instead I'm loading the category group thinking that the ID provided there is what I need, apparently it is not.

Kinda of stumped on this one. Any advice?

Thanks,

**Tags:** plugin-development, categories
> **Comments:**
> **username_1**: Hey Matt - what infomormation do you have about the category? Name, level, slug?  

---

## 1 answers

### Answer by username_1 on 2015-04-20. Score: 2

Depending on what you are searching by, you can use something similar to this. You can see the properties you can search by in the documentation.
$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->group = 'CategoryGroupName';
$criteria->title = 'CategoryTitle';
$criteria->limit = 1;

$category = $criteria->find();

> **Comments:**
> **username_0**: Doesn't **craft()->elements->getCriteria(ElementType::Category);** technically load all categories? If so I want to avoid the overhead.  
> **username_1**: Nope! It loads an element criteria model of type Category. That simply allows you to query against Category properties like group, title, level, etc. ([source](https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/ElementsService.php#L33))  
> **username_0**: Oh ok. Cool. I'll give this a try.  
> **username_0**: Question: How would I get only the children of a given category? What I mean is... I have a list of countries and under them their states/provinces. Is it possible to do something like **$criteria->children = 1**? I've tried this and it doesn't work.  
> **username_1**: You can limit the search to what [`level`](https://buildwithcraft.com/docs/templating/craft.categories#level) the category is. So if you know you just want categories of the second level (states/provinces), you can add `$criteria->level = 2` before you run `find()`  
> **username_0**: I've done that but I also get the states/provinces from other countries. I assume that **$criteria->level = 2** gets all level 2 items regardless of the parent. At least that has been my experience.  
> **username_1**: That's correct. You can limit your search even more if you have the country you want the states from by adding `$criteria->descendantOf = $country`  
> **username_0**: Oh ok. It seems to me then that I have to first get the country, then use that data to get the descendantOf. Is there a way to do this all in one shot or do I need to get country then use that result on a second query?  
> **username_1**: Yeah, you will need to first get the parent category, then get the children in two separate finds. If you are going to be running these steps frequently, I highly recommend cacheing the process. You can see a simple cache example in [this service](https://github.com/aberkie/craft-summit-api/blob/master/disney/services/Disney_InterestService.php) I made for the Craft CMS summit.  
> **username_1**: No problem! If this is working for you, can you select the answer as the correct answer so we can close out your question?  
> **username_0**: Not really sure how to do that, but I marked it as userful  

---

