<p>Don't know if this is the most efficient way, but you can always get the category using the ElementsService.</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
$criteria-&gt;group = 'categoryGroup';
$criteria-&gt;slug = 'categorySlug';
$categories = $criteria-&gt;find();

$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'section';
$criteria-&gt;relatedTo = array(
    'targetElement' =&gt; $categories,
    'field'         =&gt; 'relationField'
);
$entries = $criteria-&gt;find();
</code></pre>

CC BY-SA 3.0
2016-03-23T03:13:59.407Z
2016-03-23T03:19:05.267Z
2016-03-23T03:19:05.267Z
Douglas McDonald
<p>I'm looking to get all entries from one section (say 'videos'), that are related to a specific category. The only reference to the category is the slug that is a parameter in the url:</p>

<p>....dev/videos/category-slug</p>

<p>I need to get the category id from the slug to be able to relate the videos to the category in the criteria:</p>

<pre><code>'criteria' =&gt; [
  'section' =&gt; 'videos',
  'relatedTo' =&gt; ['targetElement' =&gt; $categoryId],
],
</code></pre>

<p>I can't seem to find a way to get the properties of the category before applying the criteria. Is there something on the EntriesService object that I can use to get the category entry, similar to template usage? E.g:</p>

<pre><code>$categoryEntry = craft()-&gt;entries-&gt;section('categories')-&gt;slug('category-slug');
</code></pre>

<p>And then pass the $categoryEntry id through to the criteria to return all the related videos. E.g:</p>

<pre><code>'criteria' =&gt; [
  'section' =&gt; 'videos',
  'relatedTo' =&gt; ['targetElement' =&gt; $categoryEntry-&gt;id],
],
</code></pre>

<p>Any help with a way I could do this all in one endpoint would be great. The alternative is passing the slug into another category endpoint to get the entry, then use the response to pass the id to the above endpoint.</p>

<p>Cheers!</p>

<plugin-development><php><plugin-elementapi><ajax>
Get entry by slug with EntriesService in ElementAPI
# Get entry by slug with EntriesService in ElementAPI

(Asked by: username_0 on 2016-03-20)

I'm looking to get all entries from one section (say 'videos'), that are related to a specific category. The only reference to the category is the slug that is a parameter in the url:
....dev/videos/category-slug
I need to get the category id from the slug to be able to relate the videos to the category in the criteria:
'criteria' => [
  'section' => 'videos',
  'relatedTo' => ['targetElement' => $categoryId],
],

I can't seem to find a way to get the properties of the category before applying the criteria. Is there something on the EntriesService object that I can use to get the category entry, similar to template usage? E.g:
$categoryEntry = craft()->entries->section('categories')->slug('category-slug');

And then pass the $categoryEntry id through to the criteria to return all the related videos. E.g:
'criteria' => [
  'section' => 'videos',
  'relatedTo' => ['targetElement' => $categoryEntry->id],
],

Any help with a way I could do this all in one endpoint would be great. The alternative is passing the slug into another category endpoint to get the entry, then use the response to pass the id to the above endpoint.
Cheers!

**Tags:** plugin-development, php, plugin-elementapi, ajax


---

## 1 answers

### Answer by username_1 on 2016-03-23. Score: 1

Don't know if this is the most efficient way, but you can always get the category using the ElementsService.
$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->group = 'categoryGroup';
$criteria->slug = 'categorySlug';
$categories = $criteria->find();

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'section';
$criteria->relatedTo = array(
    'targetElement' => $categories,
    'field'         => 'relationField'
);
$entries = $criteria->find();


---

