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

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

  • 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) – KnutSv Dec 6 '16 at 15:49

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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