<p>Your field contains an object of type <code>ElementQuery</code> in order to get all <code>Categories</code> you'll need to execute it.</p>

<pre><code>$categories = $element-&gt;getfieldValue('productTheme')-&gt;all();
$categoryTitles = [];
for($categories as $category){
    $categoryTitles[] = $category-&gt;title;
}
</code></pre>

<p>Please read the <a href="https://docs.craftcms.com/v3/relations.html" rel="nofollow noreferrer">documentation</a></p>

CC BY-SA 4.0
2018-05-08T11:54:07.823Z
2018-05-08T11:54:07.823Z
Robin Schambach
<p>I want to programmatically get all the values contained in a field of type <strong>category</strong>.</p>

<p>I am doing this on <code>Elements::EVENT_AFTER_SAVE_ELEMENT</code>. </p>

<p>If I dump <code>$event-&gt;element-&gt;getFieldValue('productTheme')</code> I get the field but I did't find how to go from there.</p>

<p>My final goal is to have the categories from the category field in an array (not all the categories in the category group, but only those selected in the field).</p>

<categories><craft3><elementquery>
Craft 3 - Programatically get all the values contained in a field of type category
# Craft 3 - Programatically get all the values contained in a field of type category

(Asked by: username_0 on 2018-05-08)

I want to programmatically get all the values contained in a field of type **category**.

I am doing this on `Elements::EVENT_AFTER_SAVE_ELEMENT`.

If I dump `$event->element->getFieldValue('productTheme')` I get the field but I did't find how to go from there.

My final goal is to have the categories from the category field in an array (not all the categories in the category group, but only those selected in the field).

**Tags:** categories, craft3, elementquery

---

## 1 answers

### Answer by username_1 on 2018-05-08. Score: 1 | Accepted answer

Your field contains an object of type ElementQuery in order to get all Categories you'll need to execute it.
$categories = $element->getfieldValue('productTheme')->all();
$categoryTitles = [];
for($categories as $category){
    $categoryTitles[] = $category->title;
}

Please read the documentation


---

