Table of Contents
How to Automatically Set WooCommerce Category Thumbnails Using Product Images
In large-scale E-Commerce management, visual gaps are conversion killers. A common issue developers face is having hundreds of categories—such as various “Hardware” or “Retail” segments—without assigned thumbnails. Manually setting these is a massive drain on resources. The solution? Use the Database to do the work for you.
At 3SixT5, we specialize in Website Optimization by reducing manual tasks. Below, we provide two scripts designed to find categories with missing images and automatically assign a random product image from within that category to serve as the thumbnail.
True Digital Strategy is about efficiency. Automating your merchandising ensures your site remains visually ‘full’ and professional, which is critical for both SEO and user trust.
3SixT5
Dev Team
Option 1: The Parent Category Sweep
This first script is a surgical strike. It specifically targets Parent Categories (categories with no parent) that are currently missing a thumbnail. This ensures your high-level navigation looks perfect without affecting sub-category logic.
add_action( 'admin_init', 'set_random_category_thumbnails' );
function set_random_category_thumbnails() {
// Only run for admins to ensure site security
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$categories = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => 0, // ONLY targets Parent Categories
) );
foreach ( $categories as $category ) {
$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
// ONLY proceed if the category has NO image
if ( ! $thumbnail_id || $thumbnail_id == 0 ) {
$args = array(
'post_type' => 'product',
'posts_per_page' => 1,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $category->term_id,
),
),
);
$random_product = get_posts( $args );
if ( ! empty( $random_product ) ) {
$product_image_id = get_post_thumbnail_id( $random_product[0]->ID );
if ( $product_image_id ) {
update_term_meta( $category->term_id, 'thumbnail_id', $product_image_id );
}
}
}
}
}Option 2: The Keyword-Prioritized Script (Advanced Merchandising)
This is a more intelligent version of the script. It allows you to specify Keywords (like ‘Bundles’, ‘Plates’, ‘Sanding Discs’, or ‘Monitors’). The code will first search for a product matching those keywords to use as the thumbnail. If it can’t find a match, it falls back to a random product image as a safety net.
add_action( 'admin_init', 'set_keyword_category_thumbnails' );
function set_keyword_category_thumbnails() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$categories = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => 0,
) );
$keywords = array( 'Sanding Discs', 'Plates', 'Monitors', 'Bundles' );
foreach ( $categories as $category ) {
$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
if ( ! $thumbnail_id || $thumbnail_id == 0 ) {
$found_image_id = null;
// STEP 1: Look for specific keywords in product titles
foreach ( $keywords as $keyword ) {
$args_keyword = array(
'post_type' => 'product',
'posts_per_page' => 1,
's' => $keyword,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $category->term_id,
),
),
);
$keyword_products = get_posts( $args_keyword );
if ( ! empty( $keyword_products ) ) {
$img_id = get_post_thumbnail_id( $keyword_products[0]->ID );
if ( $img_id ) {
$found_image_id = $img_id;
break;
}
}
}
// STEP 2: Fallback to any random product if no keyword matched
if ( ! $found_image_id ) {
$args_random = array(
'post_type' => 'product',
'posts_per_page' => 1,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $category->term_id,
),
),
);
$random_products = get_posts( $args_random );
if ( ! empty( $random_products ) ) {
$found_image_id = get_post_thumbnail_id( $random_products[0]->ID );
}
}
// STEP 3: Update the category
if ( $found_image_id ) {
update_term_meta( $category->term_id, 'thumbnail_id', $found_image_id );
}
}
}
}How to Execute Safely
- Backup: Always back up your
wp_termmetatable before running bulk updates. - Placement: Paste your chosen code into your theme’s
functions.phpfile. - Trigger: Simply log into your WordPress Admin. The
admin_inithook will run the code. - Cleanup: Once your category images are populated, remove the code immediately to prevent it from running every time the dashboard loads.
The SEO & GEO Benefit
In 2026, AI models and Generative Engine Optimization (GEO) rely on structured visual data. If an LLM (Large Language Model) crawls your site to recommend a certain monitor or brand of sanding disc, having a valid category image increases your chances of being featured in rich-snippet AI results. This isn’t just a design fix; it’s an SEO necessity.
Struggling with a messy WooCommerce store?
- Clean up your technical debt with our Custom Development Services.
- Get your store AI-ready with Advanced GEO Strategies.
- Transform your visual brand with Expert Digital Design.