Get all product images, including excluded images

Get all product images, including excluded images

July 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get all product images, including excluded images

Returning All Product Images in Magento 2

In Magento 2, when working with product images, you might find that certain methods only return the active gallery images. This can be disappointing if you need to retrieve all associated images for a given product. Let’s explore how to get both active and excluded images using the API.

Understanding the Limitations of getMediaGalleryImages

The method getMediaGalleryImages is indeed useful, but it has a limitation that excludes images with the status set to ‘disabled’. This is because Magento uses the disabled flag for media gallery entries to control their visibility in the frontend and backend.

By default, when you call $product->getMediaGalleryEntries(), it only includes images with disabled = 0. If an image has been explicitly excluded by setting disabled = 1, this method won’t include it.

Getting All Images: Active and Excluded

To retrieve all images, including those that have been excluded or disabled, you can use the getMediaGalleryImages() method in combination with a custom query. Here’s how:

$productId = 42; // Replace with your product ID.
$product = $this->productRepository->getById($productId);

$mediaGalleryEntries = $product->getMediaGalleryEntries();
foreach ($mediaGalleryEntries as $entry) {
    if ($entry->getDisabled()) {
        echo 'Excluded image: ' . $entry->getFile() . PHP_EOL;
    } else {
        echo 'Active image: ' . $entry->getFile() . PHP_EOL;
    }
}

In this example, we first retrieve the product using the productRepository. We then access the media gallery entries and loop through each entry. The $entry->getDisabled() method returns a boolean value indicating whether an image is excluded or not.

If the disabled flag is set to true (1), it’s considered excluded. If it’s false (0), the image is active and visible in the frontend.

A Note on Performance

While this approach solves the problem of retrieving all images, it’s important to consider performance implications, especially for products with a large number of media gallery entries.

Using getMediaGalleryEntries() involves loading all related data into memory. If you have many excluded images or use this method frequently, ensure your system has sufficient resources to handle the load.

Conclusion

Retrieving all product images, including those that have been excluded or set as inactive, is a common requirement in Magento 2 development. By understanding how Magento manages image visibility and using the appropriate methods together with custom logic, you can achieve this effectively.

Remember, the key is to consider both the disabled flag status and the active gallery entries when you need to access all associated images for a product.

For more advanced techniques on handling media in Magento, I recommend checking out our guide on Managing Product Images.

Happy coding!

Magento 2 Catalog Database API

Our network