<p>Ok worked this one out eventually myself. If anyone else finds this helpful.</p>

<pre><code>$userinterests = craft()-&gt;users-&gt;getUserById($userid)-&gt;getContent()-&gt;getAttributes();
$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;relatedTo = array(
        'targetElement' =&gt; $userinterests['relevantFieldToCheckAgainst'],
        'field' =&gt; 'EntryFieldToCheckIn'
);
$relatedEntries = craft()-&gt;elements-&gt;findElements($criteria);
</code></pre>

CC BY-SA 3.0
2015-05-05T09:22:09.377Z
2015-05-05T09:22:09.377Z
Lettie
<p>Ok I can do this in twig but after searching high and low I can't find a good example of how to do it in a plugin.</p>

<p>I have a user profile field called interests which holds an array of what they are interested in. I want to pull out all entries that are related to the category id's held in the interests array. So in twig I do this:</p>

<pre><code>{% for interest in userinterests %}
    {% for entry in entries.relatedTo(craft.categories(interest)) %}
        &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}           
{% endfor %}
</code></pre>

<p>In my plugin function I currently have this but it is not returning anything in the array so I'm currently stuck:</p>

<pre><code>$userinterests = craft()-&gt;users-&gt;getUserById($userid)-&gt;getContent()-&gt;getAttributes();
$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;relatedTo = array(
    'sourceElement' =&gt; $categories
);
$relatedEntries = craft()-&gt;elements-&gt;findElements($criteria);
</code></pre>

<p>I'm not sure what I am supposed to be adding to the relatedTo array but it also could be something else as I'm still trying to get my head around craft.</p>

<p>Any help much appreciated.</p>

CC BY-SA 3.0
2015-04-30T18:08:52.407Z
Hi Lettie. What is `$categories` being set to?
Aaron Berkowitz
CC BY-SA 3.0
2015-04-30T19:27:26.000Z
That's kind of what I need to find out. It's not doing anything at the moment. What I want it to do is hold an array of id's of the userinterests and pull out any entries which have the id as a category in the entires table. Each entry can have multiple categories so may get pulled out more than once but I can deal with that once I get the entries returned that I want to work with. Cheers.
Lettie
<categories><relations><elementcriteriamodel>
Related to entries in categories in a plugin
# Related to entries in categories in a plugin

(Asked by: username_0 on 2015-04-30)

Ok I can do this in twig but after searching high and low I can't find a good example of how to do it in a plugin.
I have a user profile field called interests which holds an array of what they are interested in. I want to pull out all entries that are related to the category id's held in the interests array. So in twig I do this:
{% for interest in userinterests %}
    {% for entry in entries.relatedTo(craft.categories(interest)) %}
        {{ entry.title }}
    {% endfor %}           
{% endfor %}

In my plugin function I currently have this but it is not returning anything in the array so I'm currently stuck:
$userinterests = craft()->users->getUserById($userid)->getContent()->getAttributes();
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->relatedTo = array(
    'sourceElement' => $categories
);
$relatedEntries = craft()->elements->findElements($criteria);

I'm not sure what I am supposed to be adding to the relatedTo array but it also could be something else as I'm still trying to get my head around craft.
Any help much appreciated.

**Tags:** categories, relations, elementcriteriamodel

> **Comments:**
> **username_1**: Hi username_0. What is `$categories` being set to?  
> **username_0**: That's kind of what I need to find out. It's not doing anything at the moment. What I want it to do is hold an array of id's of the userinterests and pull out any entries which have the id as a category in the entires table. Each entry can have multiple categories so may get pulled out more than once but I can deal with that once I get the entries returned that I want to work with. Cheers.  

---

## 1 answers

### Answer by username_0 on 2015-05-05. Score: 2 | Accepted answer

Ok worked this one out eventually myself. If anyone else finds this helpful.
$userinterests = craft()->users->getUserById($userid)->getContent()->getAttributes();
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->relatedTo = array(
        'targetElement' => $userinterests['relevantFieldToCheckAgainst'],
        'field' => 'EntryFieldToCheckIn'
);
$relatedEntries = craft()->elements->findElements($criteria);


---

