I am trying to get all values of a specific field type from all entries. (ex.: craft\fields\PlainText)

Is there an easy way to get these values ?

1 Answer 1


I found a way:

This function checks recursively whether a field exists with the searched type, if it is a matrix field then it searches further in it.

private function filterFieldsByType($blocks, $fieldType): array
    $values = [];

    foreach ($blocks as $block) {
        foreach ($block->getFieldLayout()->getFields() as $field) {
            if ($field instanceof $fieldType) {
                $values[] = $block->{$field->handle};
            elseif ($field instanceof \craft\fields\Matrix) {
                $values[] = $this->filterFieldsByType($block->{$field->handle}, $fieldType);
    return $values;

This is the main function that returns all fields in which the searched field type exists, the whole is returned as an array where the fields with the values are located.

public function getFieldsFromEntriesByFieldType($fieldType): array
    $entries = Entry::find()->all(); //Maybe batch query for better perfomance if there is a large of amount.
    $values = $this->filterFieldsByType($entries, $fieldType);

    $result = array_merge([], ...$values);
    $result = array_filter($result);
    return array_values($result);

How to use it:

$fieldType = 'xxx\\myplugin\\fields\\MyCustomField';

$myCustomFieldsFromAllEntries = $this->getFieldsFromEntriesByFieldType($fieldType);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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