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:


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.


1 Answer 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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.