Element API display entries by category With v2 of the Element API plugin, I'm having issues with working out how to return entries that are in a particular category.

I've got the slug in the API route but can't work out what the query should be in the transformer.

Should I be doing this query in a total different way?  I keep thinking I've just gone down the wrong way to do it completely.

'api/v1/category/&lt;slug:{slug}&gt;.json' =&gt; function($slug) {

        return [
            'elementType' =&gt; Entry::class,
            'elementsPerPage' =&gt; 30,
            'criteria' =&gt; [
              'section' =&gt; 'posts',
              'relatedTo' =&gt; ['targetElement' =&gt; ***category slug($slug)***],
            ],
            'transformer' =&gt; function(Entry $entry) {
                return [
                    'title' =&gt; $entry-&gt;title,
                    'id' =&gt; (int) $entry-&gt;id,
                    'slug' =&gt; $entry-&gt;slug,
                    'postDate' =&gt; $entry-&gt;postDate
                ];
            },
            'pretty' =&gt; true
        ];
    }
Your targetElement needs to be the category object or the ID of your category.
So if the id of your category is 5 you need

'relatedTo' =&gt; ['targetElement' =&gt; 5],


If you really want to be able to fetch your category by slug (which I would not suggest since your slug can be changed in the cp but the ID can't, if someone changes the slug by accident your entire logic will break) you can create an ElementCriteriaModel to search for the category with the slug and insert the object there.

$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
$criteria-&gt;slug = $slug; 
$category = $criteria-&gt;find();


And inside your array

'relatedTo' =&gt; ['targetElement' =&gt; $category],