# Use the First Gallery Image When No Featured Image Is Set

> When a product is saved without a featured image but has gallery images, the first gallery image becomes its featured image, so it never shows a placeholder.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, products, images, data-quality
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-gallery-image-as-featured/

## Code

```php
add_action( 'woocommerce_before_product_object_save', function ( $product ) {
    if ( $product->get_image_id() || $product->is_type( 'variation' ) ) {
        return;
    }

    $gallery = $product->get_gallery_image_ids();

    if ( $gallery ) {
        $product->set_image_id( (int) reset( $gallery ) );
    }
} );
```

## Notes

Runs whenever a product is saved, whether in the admin, through the REST API or by an import, and leaves the gallery unchanged.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Use the First Gallery Image When No Featured Image Is Set" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
