I have custom attribute value in my store.

How can I get the product id based on that custom attribute value?

2 Answers 2


To fetch value of attribute Use:

First get Your Product Collection and then


e.g: $this->getProduct()->getAttributeText('capacity');



You can check this question as reference:

How to show the dropdown attibute value using getData() method in magento 2?

Hopefully, It will help you

  • Also var_dump your product collection using get_class_methods(). Will find getCapacity() methods if your attribute was correctly configured. Commented Jul 2, 2018 at 7:03

check loadByAttribute fucntion of product model Magento\Catalog\Model\AbstractModel class, it will give you idea how to get product id / object based on attribute code and value:

 * Load entity by attribute
 * @param \Magento\Eav\Model\Entity\Attribute\AttributeInterface|integer|string|array $attribute
 * @param null|string|array $value
 * @param string $additionalAttributes
 * @return bool|\Magento\Catalog\Model\AbstractModel
public function loadByAttribute($attribute, $value, $additionalAttributes = '*')
    $collection = $this->getResourceCollection()->addAttributeToSelect(

    foreach ($collection as $object) {
        return $object;
    return false;

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.