Skip to content

How to save hide image programmatically in magento 2 admin product edit?

I want to set & save the hidden product image( Hide from Product Page ) programmatically in Magento 2 in admin panel?
enter image description here

I tried this way

$product =  $this->productLoader->create()->load($productId);
$mediaAttribute = array ();
$productImage = '/Applications/MAMP/htdocs/magento245p1v1/pub/media/catalog/product/W/S/WS03-XS-Red_1219111980.jpg';
        
$product->addImageToMediaGallery($productImage, $mediaAttribute, false, false);

$product->save()

This code will create a new image file with WS03-XS-Red_1219111980_1.jpg and save to the product. But not make the image hidden.

Any files i want to look in to or any method will be highly appreciate to achieve this task please.