Skip to content

Magento 2 get custom attribute and show on phtml file

I have created a custom product attribute using a module.

$eavSetup->addAttribute(
            MagentoCatalogModelProduct::ENTITY,
            'special_feature',
            [
                'type' => 'text',
                'label' => 'Special Feature',
                'input' => 'textarea',
                'required' => false,
                'sort_order' => 12,
                'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE,
                'group' => 'Special Feature',
                'backend' => 'MagentoEavModelEntityAttributeBackendArrayBackend',
            ]
        );

The attribute is showing in the product edit page and getting saved. Now, when I try to get the value using $product->getSpecialFeature() in the .phtml file, it is not showing anything. I am looping through the product collection generated from the code below in my Block class.

public function getProductCollectionByCategories($ids)
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addCategoriesFilter(['in' => $ids]);
        return $collection;
    }