I'm trying to programatically set the "Hide from Product Page" setting on an image.

The setting can be found here: Products > Catalog > (select a product) > Images And Videos > (click on an image).

Image showing the setting.

So far, the only way I've been able to change that setting, is by doing it manually.

Here's how I currently add images to my products:

$product->addImageToMediaGallery($imagePath, ['thumbnail'], false, true);

I thought that the last parameter which is called exclude and described as mark image as excluded from product page view would do the trick, but it doesn't. Regardless of if I set the value to trueor false, the image remains visible on the product page and the checkbox is unchecked.

I've been banging my head against a wall for the better part of a day now, so any suggestions will be greatly appreciated.


You can simply change product image visibility using a database.

If you want disabled all product images so run the below query in the database.

UPDATE `catalog_product_entity_media_gallery_value` SET `disabled` = '1' WHERE `disabled` = '0';

If you want to enable all product images so run the below query in the database.

UPDATE `catalog_product_entity_media_gallery_value` SET `disabled` = '0' WHERE `disabled` = '1';


  • It works, Thank you! Mar 12 '20 at 9:31

Apparently, after introducing multiple store views, the code above hides the images in some store views, rather than all of them.

To fix this, I changed the code to

$product->setStoreId(0)->addImageToMediaGallery($imagePath, ['thumbnail'], false, true);

I had a same situation.

I imported products with images, but I wanted to hide the first image of all products on the detail pages.

(This first image is set as small_image, thumbnail_image, and swatch_image in the product import CSV. I want them to show up on the product list pages, but I do NOT want them to show up on the galley section of the product detail pages.)

I searched online, but there seem to be no easy solution to achieve this.

So, I decided to create a comnand line custom PHP script to change DB values without using Magento2 PHP classes.

(It is NOT advised to directly change DB values. Please use this solution with your own discretion. Always take DB backups before trying this solution.)


As a preparation, I checked how the DB data is changed by setting "Hide from Product Page". I changed it for one product, then compared DB dumps of BEFORE and AFTER states.

It turned out catalog_product_entity_media_gallery_value is the only table affected by this setting change.


This is the BEFORE state for product entity_id=42 enter image description here


This is the AFTER state of product entity_id=42 enter image description here


As you can see, "value_id", "stor_id", "entity_id", "position", and "disabled" are unchanged. However, the last field "record_id" is updated.

Also, the "disabled" value is changed from 0 to 1 for the 2nd entry with "position value == 1".

It turned out, every time I updated the image settings on a product, the entries on this table are re-created with new "record_id" field values.


With this knowledge, I created this command line PHP script (file name: img-update.php).


// create ID list for products

$idList = array();
$handle = fopen("img-update-product-id-list.txt", "r");
if ($handle) {
    while (($id = fgets($handle)) !== false) {
        $idList[] = rtrim($id);
$idListStr = join(",", $idList);

// create PHP array representing catalog_product_entity_media_gallery_value data

$productList = array();

$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: ". $conn->connect_error);
echo  "Connection success\n";

$sql1 = "SELECT * FROM `catalog_product_entity_media_gallery_value` WHERE `entity_id` IN (" . $idListStr . ")";
$result = $conn->query($sql1);
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $productList[] = array(
            "value_id" => $row["value_id"],
            "store_id" => $row["store_id"],
            "entity_id" => $row["entity_id"],
            "position" => $row["position"],
            "disabled" => $row["disabled"],
            "record_id" => $row["record_id"],
            "is2nd" => false,
} else {
    echo "0 results.\n";

// determine the 2nd entry for position 1 image which activates "Hide from Product Page" setting

$entityIdList = array();

foreach($productList as $key => $p) {
    if ($p['position'] == 1) {
        if (in_array($p['entity_id'], $entityIdList)) {
            $productList[$key]['is2nd'] = true;
        } else {
            $entityIdList[] = $p['entity_id'];

// update entries

$value_id = $store_id = $entity_id = $position = $disabled = "";

$sql2 = "ALTER TABLE `catalog_product_entity_media_gallery_value` DISABLE KEYS";
foreach($productList as $index => $p) {

    $value_id = $p["value_id"];
    $store_id = $p["store_id"];
    $entity_id = $p["entity_id"];
    $position = $p["position"];
    $disabled = $p["disabled"];

    $is2nd = $p["is2nd"];

    if ($is2nd) {
        $sql2 = "INSERT INTO `catalog_product_entity_media_gallery_value` (`value_id`, `store_id`, `entity_id`, `label`, `position`, `disabled`) VALUES (" . $value_id . "," . $store_id . "," . $entity_id . ",''," . $position . ",1)";
    } else {
        $sql2 = "INSERT INTO `catalog_product_entity_media_gallery_value` (`value_id`, `store_id`, `entity_id`, `label`, `position`, `disabled`) VALUES (" . $value_id . "," . $store_id . "," . $entity_id . ",''," . $position . ",0)";

    echo '+';  // progress indicator
$sql2 = "ALTER TABLE `catalog_product_entity_media_gallery_value` ENABLE KEYS";

echo "\n";

// remove old entries

$sql3 = "ALTER TABLE `catalog_product_entity_media_gallery_value` DISABLE KEYS";
foreach($productList as $index => $p) {

    $value_id = $p["value_id"];
    $store_id = $p["store_id"];
    $entity_id = $p["entity_id"];
    $position = $p["position"];
    $disabled = $p["disabled"];

    $record_id = $p["record_id"];

    $sql3 = "DELETE FROM `catalog_product_entity_media_gallery_value` WHERE `value_id` = " . $value_id . " AND `store_id` = " . $store_id  . " AND `entity_id` = " . $entity_id . " AND `position` = " . $position . " AND `record_id` = " . $record_id;
    echo '-';  // progress indicator
$sql3 = "ALTER TABLE `catalog_product_entity_media_gallery_value` ENABLE KEYS";

echo "\n";



There is a companion text file used with this PHP script. The file name is img-update-product-id-list.txt.



Place both img-update.php and img-update-product-id-list.txt file to the root directory of Magento 2 installation, and run the script as php img-update.php from your terminal.

Modify the .txt and .php files to suite your setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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