How to get products by specific attributes in Magento 2

How to get products by specific attributes in Magento 2

August 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get products by specific attributes in Magento 2

Retrieving Products by Specific Attributes in Magento 2: A Comprehensive Guide

In Magento 2, retrieving products based on specific attributes involves interacting with the Magento product model and using its capabilities for filtering attributes. Let’s dive into how you can achieve this.

Understanding the Product Model

The Magento\Catalog\Model\Product is the core class that represents a product in the Magento architecture. It provides methods to load, save, update, delete, and perform various operations on products. To work with products programmatically, we need to interact with this model.

Loading Products by ID

In your query, you’ve already demonstrated how to load a product by its ID using the load() method of the Magento\Catalog\Model\Product class:

$product_id = 2047;
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);

This code loads the product with an ID of 2047. However, loading a single product doesn’t help us filter products based on specific attributes.

Defining the Attribute IDs

In your question, you’ve identified the attribute IDs as 54 for color and 170 for size:

$attr_color = 54;
$attr_size = 170;

These are the unique identifier numbers assigned to the attributes in the Magento admin panel. We’ll use these identifiers to filter products.

Filtering Products by Attributes

To filter products based on specific attribute values, we need to create a collection and apply filters using the addFieldToFilter() method. This allows us to query for products that match the given attribute-value pairs:

$collection = $product->getCollection();
$collection->addFieldToFilter('attribute_set_id', 4); // Assuming attribute set ID is 4
$collection->addFieldToFilter('color', array('eq' => $attr_color)); // Filter by color attribute (ID:54)
$collection->addFieldToFilter('size', array('eq' => $attr_size)); // Filter by size attribute (ID:170)

foreach ($collection as $product) {
    echo 'Product ID: '. $product->getId() . ', Name: '. $product->getName();
}

In this example, we create a product collection and apply filters for the attribute_set_id, color (ID 54), and size (ID 170) attributes. The addFieldToFilter() method accepts an array with the filter parameters (‘eq’ stands for equality).

Conclusion

Retrieving products by specific attributes in Magento 2 involves loading the product collection, applying filters using the addFieldToFilter() method, and iterating through the results to access the desired information.

Remember that direct object manager usage is generally discouraged due to performance implications. It’s better to use dependency injection provided by Magento’s framework to get instances of the required classes:

public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory  // Inject product collection factory
)
{
    $this->productCollectionFactory = $productCollectionFactory;
}

Then, you can retrieve the collection by calling $this->productCollectionFactory->create().

For a more detailed guide on Magento product filtering and performance best practices, consider reading our in-depth article at https://laravelcompany.com/magento-product-filtering-performance.

Magento 2 Catalog Database Admin

Our network