<p>You can create 2 queries, first you pull all the channel entries, that are related to your <code>bandCategory</code></p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;relatedTo = $bandCategory;
$criteria-&gt;section = 'channel';
$relatedChannelEntries = $criteria-&gt;ids();

// then continue your code 
$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'stories';
$criteria-&gt;search = $this-&gt;query;
$criteria-&gt;order = $this-&gt;orderSql();
$criteria-&gt;limit = 1000;
$criteria-&gt;visibility = $this-&gt;checkVisibility();
$criteria-&gt;thisIsAGuide = ':empty:';
// and search only for entries that are related to channel entries that are related to your band
$criteria-&gt;relatedTo = $relatedChannelEntries;
</code></pre>

CC BY-SA 3.0
2018-01-29T22:59:13.627Z
2018-01-29T22:59:13.627Z
Robin Schambach
<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'stories';
$criteria-&gt;search = $this-&gt;query;
$criteria-&gt;order = $this-&gt;orderSql();
$criteria-&gt;limit = 1000;
$criteria-&gt;visibility = $this-&gt;checkVisibility();
$criteria-&gt;thisIsAGuide = ':empty:';
</code></pre>

<p>I need help adding a relatedTo criteria to help pull related entries.</p>

<p>The current search scope is searching on entries ("Story").
The entry("Story") contains an entry ("channel"). 
That channel has a category("brandCategories")</p>

<p>I need to return stories that contain entries that have a matching brandCategory.
That brandCategory is getting pulled from a GET var of "&amp;brands%5B%5D={{categoryId}}</p>

<search><relatedto>
RelatedTo in plugin based search
# RelatedTo in plugin based search

(Asked by: username_0 on 2018-01-29)

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'stories';
$criteria->search = $this->query;
$criteria->order = $this->orderSql();
$criteria->limit = 1000;
$criteria->visibility = $this->checkVisibility();
$criteria->thisIsAGuide = ':empty:';

I need help adding a relatedTo criteria to help pull related entries.
The current search scope is searching on entries ("Story").
The entry("Story") contains an entry ("channel"). 
That channel has a category("brandCategories")
I need to return stories that contain entries that have a matching brandCategory.
That brandCategory is getting pulled from a GET var of "&brands%5B%5D={{categoryId}}

**Tags:** search, relatedto


---

## 1 answers

### Answer by username_1 on 2018-01-29. Score: 2

You can create 2 queries, first you pull all the channel entries, that are related to your bandCategory
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->relatedTo = $bandCategory;
$criteria->section = 'channel';
$relatedChannelEntries = $criteria->ids();

// then continue your code 
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'stories';
$criteria->search = $this->query;
$criteria->order = $this->orderSql();
$criteria->limit = 1000;
$criteria->visibility = $this->checkVisibility();
$criteria->thisIsAGuide = ':empty:';
// and search only for entries that are related to channel entries that are related to your band
$criteria->relatedTo = $relatedChannelEntries;


---

