How can I pull out other entries related by tag using the Element API?
In this particular case I'm using the Similar plugin to pull out related entries by tag in the templates. I'm not sure if this would help to provide an easier way or not...?
How can I pull out other entries related by tag using the Element API?
In this particular case I'm using the Similar plugin to pull out related entries by tag in the templates. I'm not sure if this would help to provide an easier way or not...?
The Similar plugin allows access to its find()
method from PHP and your Element API configuration file.
$similarEntriesByTags = craft()->similar->find([
element => $entry,
context => $entry->tags,
criteria => $limitCriteria,
]);
This is the equivalent to the Twig example code in the readme.
{% set similarEntriesByTags = craft.similar.find({ element: entry, context: entry.tags }) %}
If you’re inexperienced in quering for entries or using element criteria in PHP in general have a look a the Working with Elements article in the Craft documentation.