<p>Here's some code that should return categories related to a user:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
$criteria-&gt;relatedTo = craft()-&gt;userSession-&gt;getUser();

return craft()-&gt;elements-&gt;findElements($criteria);
</code></pre>

CC BY-SA 3.0
2016-12-06T15:56:14.830Z
2016-12-06T15:56:14.830Z
KnutSv
<p>I'm building a service where a few user attributes (category fields) are needed and have to be compared against an entry. </p>

<p>However, when I get the current user and try to load these attributes, they show as <code>NULL</code>. They are filled in at the user's profile, so they shouldn't be <code>NULL</code>.</p>

<p>Below is the code I have used to get try and get the attributes values.</p>

<pre><code>$user = craft()-&gt;userSession-&gt;getUser();
$user-&gt;getContent()-&gt;getAttributes()
</code></pre>

<p>Some values are filled out, like the custom field for postal code and phone number. However those category fields are all <code>NULL</code>.</p>

CC BY-SA 3.0
2016-12-06T15:49:15.563Z
Guess the additional parenthesis ")" inside getName is a typo? Anyways, if you use categories I think they're added to a relations table, and not to the user table. So you'd have to query for $categories->relatedTo($user)
KnutSv
<plugin-development>
Getting custom user attributes in a service, they return NULL
# Getting custom user attributes in a service, they return NULL

(Asked by: username_0 on 2016-12-06)

I'm building a service where a few user attributes (category fields) are needed and have to be compared against an entry. 
However, when I get the current user and try to load these attributes, they show as NULL. They are filled in at the user's profile, so they shouldn't be NULL.
Below is the code I have used to get try and get the attributes values.
$user = craft()->userSession->getUser();
$user->getContent()->getAttributes()

Some values are filled out, like the custom field for postal code and phone number. However those category fields are all NULL.

**Tags:** plugin-development

> **Comments:**
> **username_1**: Guess the additional parenthesis ")" inside getName is a typo? Anyways, if you use categories I think they're added to a relations table, and not to the user table. So you'd have to query for $categories->relatedTo($user)  

---

## 1 answers

### Answer by username_1 on 2016-12-06. Score: 2

Here's some code that should return categories related to a user:
$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->relatedTo = craft()->userSession->getUser();

return craft()->elements->findElements($criteria);


---

